Timing first call of setInterval differently than the rest

I am trying to time the first call of setInterval differently then the rest, so that it takes 100 seconds before the first call and then 5 seconds between all the rest. This is my code:

setTimeout((setInterval(test,5000)),100000);

function test(){
   alert("hi!");
}

JSFiddle

Upvotes: 0

Views: 64

Answers (1)

Anders
Anders

Reputation: 8577

The first argument needs to be a function. You are using (setInterval(test,5000)). That is not a function, but a statement that runs immediately so that the JS engine can evaluate the result and pass it to setTimeout, in the same way it would immediately evaluate (3+3) and pass 6 to the function if you had used setTimeout((3+3), 100000) instead.

So the solution is to pass a function:

setTimeout(function() { setInterval(test,5000); },100000);

function test(){
   alert("hi!");
}

An unrelated tip: Use console.log() instead of alert() for debugging like this, so you don't have to click away alert boxes all of the time.

Upvotes: 2

Related Questions