Reputation: 8548
I have some behavior attached to a keyup event in my jQuery plugin. Everything works fine except the function supposed to trigger AFTER the timeout is executed immediately.
//attach keyup event for the searchbox
self.data('gallery_searchTimeout',null); //to enable a delay on the keypress search event
$(self).find('.gallery-search').keyup(function(){
var needle = 'look for me';
var delay = 2000;
//if a timer is already set, clear it in favor of our new timer
if (self.data('gallery_searchTimeout') != undefined) clearTimeout(self.data('gallery_searchTimeout'));
self.data('gallery_searchTimeout', setTimeout((self.data('search')(self,needle)), delay) );
});
var search = function(self,needle) {
console.log('search called with needle: '+needle);
};
self.data('search',search);
Upvotes: 0
Views: 344
Reputation: 382170
Pass the function to setTimeout
instead of calling it.
Change
self.data('gallery_searchTimeout', setTimeout((self.data('search')(self,needle)), delay) );
to
self.data('gallery_searchTimeout', setTimeout(self.data('search').bind(null,self,needle), delay) );
or
self.data('gallery_searchTimeout', setTimeout(function(){
self.data('search')(self,needle);
}, delay) );
Upvotes: 1