Skip to content


The timers' ms arg should be an integer #897

wants to merge 1 commit into from

4 participants

xk commented

setTimeout() / setInterval() do strange things when it isn't.

@xk xk the timer's ms arg ought to be an integer
Timers do strange things when it isn't.

Patch LGTM.

What strange things did you observe?

Anyway, I just tested and setTimeout seems to accept timeouts as strings in the browser, so doing it the same in node is probably a good idea.

xk commented

The weird thing that happens when passing a float is that it takes longer to fire than expected. This demonstrates the problem

You can pass strings:
[Math.floor('3.14'), typeof Math.floor('3.14')] -> [ 3, 'number' ]


Seems like the Bad Thing happens when passing in a number between 0 and 1. JS doens't have sub-millisecond precision anyhow, so flooring is the right thing to do.

ry commented

i don't think there is any problem. we already take the IntegerValue in the binding.


@ry: Somehow it ends up doing a setTimeout for 1000 when provided a number between 0 and 1. Try it:

var t =
setTimeout(function () {
  console.error( - t)
}, 0.1)

When provided with a float greater than 1, it works as expected.

ry commented

ah okay. then it is a bug in the binding and should be fixed there. test should be added too.

@ry ry added a commit that closed this pull request
@ry ry Fix timeouts with floating point numbers bug
fixes #897.
@ry ry closed this in c8e447e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 11, 2011
  1. @xk

    the timer's ms arg ought to be an integer

    xk committed
    Timers do strange things when it isn't.
Showing with 2 additions and 1 deletion.
  1. +2 −1 lib/timers.js
3 lib/timers.js
@@ -148,6 +148,7 @@ = function(item) {
exports.setTimeout = function(callback, after) {
var timer;
+ after = Math.floor(after);
if (after <= 0) {
// Use the slow case for after == 0
timer = new Timer();
@@ -201,7 +202,7 @@ exports.clearTimeout = function(timer) {
exports.setInterval = function(callback, repeat) {
var timer = new Timer();
+ repeat = Math.floor(repeat);
if (arguments.length > 2) {
var args =, 2);
timer.callback = function() {
Something went wrong with that request. Please try again.