29

In my application I found some JavaScript code that is using setInterval with 0 milliseconds, like so:

self.setInterval("myFunction()",0);

Obviously, this does not seem like a good idea to me. Can anyone tell me what will be the behaviour of setInterval here? ("myFunction" makes an AJAX call to the server)

I am asking this because I am having an irregular behaviour in my application. 90% of the times, the application behaves correctly and exactly one call to the server is made. However sometimes, multiple calls are made to the server (until now, maximum is 48 calls) and I am almost certain it is the fault of this line of code.

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
João Gomes
  • 371
  • 1
  • 4
  • 6

5 Answers5

28

Browser set a minimal value for the interval. Usualy 10ms, but it can depend on the browser. This means repeat this as fast as I'm possibly allowed. The W3C spec say 4ms : http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers

This is correct but probably reveal a design error.

EDIT: By the way, it is bad practice to pass a string to setTimeout/setInterval, pass a function instead as javascript has first class functions.

deadalnix
  • 2,255
  • 18
  • 24
1

setInterval(myFunction, 0) calls myFunction continuously with minimum delay. It is almost like calling myFunction in a infinite loop. Except that here you can stop the loop using the clearInterval method.

Adam Chwedyk
  • 418
  • 4
  • 8
Narendra Yadala
  • 9,554
  • 1
  • 28
  • 43
  • @Brad I thought the "except it can be cleared with `clearInterval`" was important :P – Joseph Marikle Oct 04 '11 at 13:27
  • I was thinking that it was a bug with SO when doing multiple edits it rolls back one or something :P – Narendra Yadala Oct 04 '11 at 13:28
  • @Joseph, what do you mean? I didn't edit anything out of his answer, I just added the code tags around the relevant parts. – Brad Oct 04 '11 at 13:28
  • 1
    Wrong, see my answer to know why and avoid to answer what you don't know in the future, this is worse than saying nothing because it's misleading. – deadalnix Oct 04 '11 at 13:29
  • @Brad must have been a bug like Narendra said then. It just shows up as you deleting it in the edit history. – Joseph Marikle Oct 04 '11 at 13:29
  • @Narendra, Ah, that is probably what happened. Sorry about that. I had no intention of removing part of your answer. Usually when that happens, a popup appears saying that someone else just edited it. Perhaps we submitted near-simultaneously. – Brad Oct 04 '11 at 13:29
  • @deadalnix I executed the code with setInterval 0 delay and saw that the function executes in a loop with no delay between the invocations. It went on till i cleared the interval. I don't understand where is the issue. – Narendra Yadala Oct 04 '11 at 13:36
  • If your function uses 15ms to run, and your browser minimum interval is 10ms, you'll see a recall with no pause (different from infinite loop, because some other event can be trigered between calls) but otherwize, a pause will occurs. – deadalnix Oct 04 '11 at 13:38
  • @deadalnix I am using the phrase 'infinite loop' only to illustrate/explain the concept of invoking setInterval with 0ms as delay param. It does not mean there is no pause. Even an infinite loop has a pause because of OS scheduling and other factors. – Narendra Yadala Oct 04 '11 at 14:13
0

setInterval with '0' moves the code execution at the end of the current thread. The code is put to the side, all other code in the thread is executed, and when there is no code for execution, then the side code is executed.

niio
  • 326
  • 3
  • 15
0

To have it executed only once with minor delay, use setTimeOut instead:

window.setTimeout(myFunction, 10);

As you're using AJAX, you don't have to use any timers at all - just call the next AJAX request in the Callback (complete/success event) of the current AJAX request.

Post your current code and we might be able to guide you further.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • I believe that setTimeout() is what the coder meant to do, because it does not make sense here to be calling the server multiple times. – João Gomes Oct 04 '11 at 13:40
0

I assume that in myFunction() there is a clearInterval.

Basically, you've set an interval that can happen as often as possible. If the browser executing JavaScript actually gets to the clearInterval part before the next iteration of the interval, then it will be fine. Otherwise, it will happen again and again.

Use setTimeout instead.

Brad
  • 159,648
  • 54
  • 349
  • 530