This is an external api but will require changes be made to the interpreter. I think the timeout should be potentially executed after the execution of a statement.
There is only one executing thread of code at a time but multiple 'threads' need to be active in the program context at a time. Some higher level controller then selects the thread to execute next.
For example:
var x = 0;
var y = 0;
function inc(){ ++x; };
setTimeout(f, 1); // Assume time is a number of statements
// Creates a new 'thread' that will call f.
// Check after setTimeout expr statement done to see if should execute.
// Finds zero so no
++y;
// Check again, finds one so yes.
// f executed before next statement. f thread removed from active threads.
// x is 1 here;
++y;
++y;
This is an external api but will require changes be made to the interpreter. I think the timeout should be potentially executed after the execution of a statement.
There is only one executing thread of code at a time but multiple 'threads' need to be active in the program context at a time. Some higher level controller then selects the thread to execute next.
For example: