tags:

views:

28

answers:

2
  setInterval(func, 1000)

What if the func takes longer than 1000 ms, will the next call wait for 1000 ms or execute right away?

A: 

The callback method will be invoked every 1000ms if there's nothing much else going on.

I have half a piece of code written out to elaborate a way to call an interval every 1000ms, and depending on a boolean value the interval re-invokes or deletes itself. Let me know if you want me to paste.

Edit: See answer from bhups, much better description - still let me know if you want that code I mentioned.

Danjah
+1  A: 

Flash runtime is single threaded. So whenever there is an action to perform (a function call) then it get queued to the already pending actions. So at any point of time only one function is being executed.
In your case If you function func is taking more than 1000 ms to execute i.e. if func is in between its execution and time interval expires a new call to func is queued. So as soon as first func is finished executing then the next call will be picked if there is any. In this case it will be another func call. If there were other listener which were called before this then they will be called in order.

bhups
Just to be clear, while this function is taking 1000ms to execute, Flash will be doing nothing else whatsoever - no timers, no mouse events, no screen updates.
fenomas
@fenomas: Agreed! Thats why it is important to architect your solution in a proper way. Don'y try to do everything in a single big function. Try to make it more event driven.
bhups