I am using JavaScript with jQuery. I have the following script to alert hi
every 30 seconds.
$(document).ready( function() {
alert("hi");
setInterval(function() {
alert("hi");
}, 30000);
});
I want to alert hi
when the page loads (when document / page gets completely loaded) and on every 30 seconds interval afterwards (like hi
(0s) - hi
(30s) - hi
(60s).. etc). But my solution works on two instances. One on DOM ready and the other in a loop. Is there any way to do the same in a single instance?
You can see my fiddle here.
JavaScript interval to be set use setInterval() function. It could be defined simply as a method which allows you to invoke functions in set intervals. The JS setInterval() function is similar to the setTimeout method.
setInterval is not a jQuery function. It is a builtin JavaScript function.
In JavaScript, a block of code can be executed in specified time intervals. These time intervals are called timing events. There are two methods for executing code at specific intervals. They are: setInterval()
You could use setTimeout instead and have the callback reschedule itself.
$(function() {
sayHi();
function sayHi() {
setTimeout(sayHi,30000);
alert('hi');
}
});
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