14

I've the following code which runs fine under Chrome (V8) but fails inside node:

var id;
id = setTimeout("TimeoutHandler()", 10);
console.log ('SET');

function TimeoutHandler()
{
  clearTimeout(id);
  console.log ('CLEAR');
}

Chrome output:

SET 
CLEAR 

Nodejs output:

SET
timers.js:110
    first._onTimeout();
          ^
TypeError: Property '_onTimeout' of object [object Object] is not a function
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Any ideas why? Thanks

1
  • Interesting that NodeJS doesn't type-check the argument. It could provide a clearer error message if it did. Commented May 9, 2013 at 17:32

1 Answer 1

23

Unlike in most browsers, setTimeout in node.js does not accept a string parameter. You must pass it a function. For example:

function TimeoutHandler()
{
  clearTimeout(id);
  console.log ('CLEAR');
}

var id;
id = setTimeout(TimeoutHandler, 10);
console.log ('SET');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.