Thursday, 15 January 2015

javascript - setTimeout behaviour with blocking code -



javascript - setTimeout behaviour with blocking code -

this test code (fiddle here):

console.log('before wait'); settimeout(function () { console.log('yo!'); }, 1000); var start = date.now(); while (date.now() < start + 3000) {} console.log('after wait');

this timeline of events in chrome:

time 0 seconds: prints "before wait" time 3 seconds: prints "after wait", , after "yo!"

is behaviour according spec? why not

time 0 seconds: prints "before wait" time 3 seconds: prints "after wait" time 4 seoncds: prints "yo!"

?

the delay of settimeout relative exact point in time when called. expires while still busy waiting. performed @ next instant command goes event loop.

edit:

the spec bit vague in point, guess it's intended , straightforward interpretation:

settimeout(function, milliseconds)

this method calls function 1 time after specified number of milliseconds elapses, until canceled phone call cleartimeout. methods returns timerid may used in subsequent phone call cleartimeout cancel interval.

javascript settimeout

No comments:

Post a Comment