A simple wait-to-trigger library that will suspend a function from being activated while an event gets continuously triggered.
- There are none
- AMD
- CommonJS
- Globals
bower install suspend.js
Prevent a function from being actived when keys keep getting pressed
var search_wait = new Suspend();
search_wait.callback(function() {
console.log("If key was pressed more than once every 200ms then I wouldn't have activated!");
});
document
.getElementById("search-input")
.onkeypress = function() {
search_wait.every(200).start();
};
Or figure out what to do when the window stops being resized
var responsive_wait = new Suspend();
window.onresize = function(event) {
responsive_wait
.callback(function() {
console.log("If window is resized more than once every 1000ms then I won't activate!");
})
.every(1000)
.start();
};
Delay function until mouse is not longer hovering over either element
var hover_wait = new Suspend({
element: [
document.getElementById("box-one"),
document.getElementById("box-two")
],
event: "onmouseover",
callback: function(context, e) {
console.log(context, e);
console.log("If either box was hovered more than once every " + context.opts.every + "ms then I wouldn't have activated!);
}
}).start();
- options -- object that will override and default settings
- element -- DOM element to apply event to, also accepts an array of elements
- event -- string specifying the event: "onclick" or "onmouseover", also accepts an array of events
- cb -- function to be activated when the timer expires
- dt -- data object that will be passed to the callback function
- timer -- integer in miliseconds that determines how long to wait for the event to be triggered before activating the callback function
- timer -- integer in miliseconds that determines how long to wait for the event to be triggered before activating the callback function
Initiates Suspend
- timer -- integer in miliseconds that determines how long to wait for the event to be triggered before activating the callback function
Alias for start()
- Eric Bower neurosnap@gmail.com