codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

jQuery assign event handler with timeouts per element


By : , Category : javascript

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);
});
ReLated :

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 .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .