I am working on a web project, and I need a loop that will loop every 5 or 6 seconds. I want to be able to activate a block of code to change part of the page. How can I do this without crashing the page?
2 Answers
JavaScript has a native setInterval function that allows you to run a specific block of code at a defined interval.
Try something like this:
var someFunction = function(){
// define your loop here
};
var interval = 5000; // 5000 milliseconds is 5 seconds.
setInterval(someFunction, interval);
Comments
You should use the native setInterval function
You can get more information here: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval
Say the block of code you wish to activate is a function called everyFiveSeconds
Your code would be:
setInterval(everyFiveSeconds, 5000);
The interval is counted in milliseconds
.delay()function api.jquery.com/delay