0

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?

3
  • Wasn't really sure how to say it... Commented Jun 17, 2015 at 0:42
  • 3
    Do you have any code already written? Give us something to go off of. Commented Jun 17, 2015 at 0:42
  • Look into jQuery timer plugin or .delay() function api.jquery.com/delay Commented Jun 17, 2015 at 0:46

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.