jQuery assign event handler with timeouts per element


You could store the time variable using jQuery's data method which can store data on each of your elements.

$('body').on('mousemove', '.cont', function(){

  var thiis = $(this),
      // get the time from the data method
      time = thiis.data("timer-id");

   if (time){
     clearTimeout(time);
   }

  thiis.addClass('showControls');

   var new_time = setTimeout(function(){
    thiis.removeClass('showControls');
   }, 2000);

   // save the new time
  thiis.data("timer-id", new_time);
});

One simple and direct approach is to do what you always do when you want to reuse functionality: Make it a function you can call from more than one place:

// The original handler
function originalHandler() {
    alert('I was clicked');
}

// Hooking it up, not including the special element
$('.element:not(.specific_extended_element)').click(originalHandler);

// Hooking up your additional handler
$('.element.specific_extended_element').click(function(e){
    alert('Extended click');
    originalHandler.call(this, e);
});

Live Example

When initially creating the array you could instead add a unique CSS class to each of them. This would allow you to use a jQuery selector to return all the elements and bind the additional behavior to them all at once.

If you need to add or remove elements from the array you could add or remove the class from the elements. You can cache the results of the jQuery selector for similar performance to the array you're using.

You could delegate it to modal div level:

$('#commentDelete').on('click', '#confirmDeleteComment', function () {
    console.log("!!!");
});

You can use jQuery.on method. It can attach handlers to all existing in the DOM and created in future tags of the selector. Syntax is as follows:

$(document).on('click', '.toBuyItem', function(){
//do onClick stuff
})
Comments


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .