0

Exact Duplicate:
how can I execute javascript code every a specific time interval ?

Is there an efficient method for causing a javascript (ChangeDivBG(), in my case) function to execute every fixed time interval?

Community
  • 1
  • 1
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • hamlin, if that one wasn't enough, then how about: http://stackoverflow.com/questions/4836862/how-can-i-execute-javascript-code-every-a-specific-time-interval http://stackoverflow.com/questions/914951/show-and-hide-divs-at-a-specific-time-interval-using-jquery http://stackoverflow.com/questions/2511952/execute-function-every-nth-second http://stackoverflow.com/questions/6081443/run-a-function-in-time-interval-in-jquery How many duplicates must there be before there are enough for people to find? – davin Jun 08 '11 at 16:27
  • Voted to close, exact duplicate: http://stackoverflow.com/questions/4836862/how-can-i-execute-javascript-code-every-a-specific-time-interval – Brian Webster Jun 08 '11 at 16:57
  • I apologise if my tone was too harsh, I find **far** too many people asking questions *before* they've tried very hard to find an answer by themselves, and I prefer (maybe wrongly on my part) to scold than to be gentle. Although I don't think I intended on that comment to be read as harshly as you may have read it; I apologise if there was a misunderstanding. You're right in general though, my attitude is rather miserable. I thought that's why I enjoyed working with computers and not people; but in that case I should stay away from SO. – davin Jun 08 '11 at 17:21
  • I agree with your frustration. I think an appropriate discussion may be why didn't the exact duplicate show up in the auto-search when I posted the question? Perhaps it was my use of hyphens on fixed-time-interval. You can paste the question title into the ask-box of a new question and see for yourself -- nada. – Brian Webster Jun 08 '11 at 17:24
  • I believe you. It's a known issue that SO doesn't have a good search facility (http://meta.stackexchange.com/questions/5622/example-of-really-bad-search-in-so although there's a better meta discussion somewhere), but google works wonders with SO questions. I'm not aware of a valid reason not to google the same thing in 10 different ways to try and find an answer... – davin Jun 08 '11 at 17:38

3 Answers3

3
setInterval(ChangeDivBG, 1000); // 1000 is time interval in miliseconds - 1000ms=1sec
mkilmanas
  • 3,395
  • 17
  • 27
2

You can use setInterval function

setInterval(ChangeDivBG, delay);

If you want to pass an argument:

setInterval(function(color){ ChangeDivBG(color) }, delay);

If you pass an argument like this:

setInterval(ChangeDivBG, delay, color);

It won't work in IE(under IE9), be careful about that.

Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
0

use setInterval (func, t).

setInterval(ChangeDivBG, 1000)

t is in milliseconds.

Tamzin Blake
  • 2,594
  • 20
  • 36