JavaScript - jQuery interval JavaScript - jQuery interval jquery jquery

JavaScript - jQuery interval


You could use setTimeout instead and have the callback reschedule itself.

$(function() {    sayHi();    function sayHi() {       setTimeout(sayHi,30000);       alert('hi');    }});


Wrap your code in a function. Then, pass the function as a first argument to setInterval, and call the function:

$(document).ready( function() {    //Definition of the function (non-global, because of the previous line)    function hi(){        alert("hi");    }    //set an interval    setInterval(hi, 30000);    //Call the function    hi();});


Well, there probably is a way of doing it in just one call..

setInterval(    (function x() {        alert('hi');        return x;    })(), 30000);

Another solution would be to use arguments.callee, but as it is now deprecated, it is generally not recommended to use it.

setInterval(    (function() {        alert('hi');        return arguments.callee;    })(), 30000);