I want to schedule events, that will fire and call my predefined callback
How can schedule in js / jquery:
You want setTimeout for a one time event and setInterval for a repeating event.
Both take two arguments: a function and an interval of time specified in milliseconds.
var delay_millis = 1500;
//will alert once, at least half a second after the call to setTimeout
var onceHandle = window.setTimeout(function() {
alert("Time has passed!");
}, delay_millis);
//will alert again and again
var repeatHandle = window.setInterval(function() {
alert("Am I annoying you yet?");
}, delay_millis);
Bonus: if you keep around the values returned by calling these functions, you can cancel the callback if you need to.
var shutUpShutUp = function() {
window.clearInterval(repeatHandle);
};
shutUpShutUp(); //now I can hear myself think.
jQuery has nothing to do with it. You need JavaScript timers: setTimeout() and setInterval()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With