I want to execute code every 10 seconds, but also on page load. I mean I want the code to execute when the page loads initially then every 10 seconds. The following code only executes the code initially after 10 seconds.
window.setInterval(function(){
/// call your function here
}, 10000);
Thanks!
You can do this :
(function(){
var f = function() {
// do something
};
window.setInterval(f, 10000);
f();
})();
The IIFE is used here to avoid polluting the enclosing namespace.
First execute the function in $.ready
and then start the interval with that same function.
Something along the lines of:
$(function() {
var f = function() { };
f();
window.setInterval(f, 10000);
});
Don't inline the function, and then just call it immediately:
window.setInterval(foo, 10000);
foo();
function foo()
{
//Do Stuff
}
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