Skip to content
Advertisement

add another timer on already running loop

Given the following program –

JavaScript

The second timer handle is never run on the loop, since the loop is already running, and “Timer2 called” is never printed. So I tried stopping the loop temporarily after running it and then adding the second timer –

JavaScript

But this again didn’t work, probably because the later lines won’t be executed after 1st loop starts running with an repeating timer. So how should I add a new timer handle to already running uvloop?

Advertisement

Answer

You are right that loop needs to be stopped before it can register a new handle. It cannot be achieved by calling uv_stop function right after uv_run, because uv_run needs to return first. It can be achieved for example by stopping it using a handle callback. Here is quite silly example of how it can be done using the existing Timer1 handle. It stops the loop exactly one time on the first run.

JavaScript

So the output is

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement