Skip to content

setTimeout callback executed before delay elapses #10154

Closed
@marzelin

Description

@marzelin
  • Version: 7.2.0
  • Platform: Ubuntu 16.10 64-bit

When the delay is set to 1, for about 5% of cases the logged time between executing setTimeout() and the callback execution is 0.

// main.js
const timeJustBeforeCallingSetTimeout = Date.now();
setTimeout(() => {
  const callbackExecutionTime = Date.now();
  console.log(callbackExecutionTime - timeJustBeforeCallingSetTimeout);
}, 1);
# bash script that runs above code 1000 times and counts cases when 0 is logged
rm -f results.txt
for ((n=0; n<1000; n++))
do
  node main.js >> results.txt
done
cat results.txt | grep 0 | wc -l

When the delay value is set to 2, there are no cases where the logged time is less than 2, which is an expected behavior.

Metadata

Metadata

Assignees

Labels

blockedPRs that are blocked by other issues or PRs.libuvIssues and PRs related to the libuv dependency or the uv binding.questionIssues that look for answers.timersIssues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions