1

In javascript we use setInterval functions like this.

myInteval= setInterval("func",t);

What if the execution time of "func" itself is greater than interval time t ?

I think js is single threaded. How is this achieved ??

6
  • 2
    JS is not a single thread. You can fire multiple functions in the same time Commented May 9, 2014 at 13:16
  • 1
    There's no true threading in JavaScript. Commented May 9, 2014 at 13:17
  • 4
    No java script is single threaded... when we execute js code, it can only happen from one thread at a time. Commented May 9, 2014 at 13:18
  • 3
    javascript is single threaded..view this post on stackoverflow stackoverflow.com/questions/21718774/… Commented May 9, 2014 at 13:18
  • 2
    @murali, please read this. Commented May 9, 2014 at 13:19

2 Answers 2

7

Then it will wait until func has finished executing, check the queue of functions to run on an interval, then run it again.

See the event loop for more details.

Sign up to request clarification or add additional context in comments.

1 Comment

Will the browser drop queued functions ? as it also has limits ? if they are dropped which function will be dropped first ?
1

A few important things from this piece by John Resig:

http://ejohn.org/blog/how-javascript-timers-work/

...timer delay is not guaranteed...

Which means, it is not essential that the t that you specify will be honoured as is. It indicates a minimum time and not a guaranteed time.

Further down:

...Intervals don’t care about what is currently executing, they will queue indiscriminately, even if it means that the time between callbacks will be sacrificed...

So, effectively the func will be queued to be executed without any t delay if the queue is accumulated due to execution.

And is summarized at the end:

...Intervals may execute back-to-back with no delay if they take long enough to execute (longer than the specified delay).

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.