Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswomack/9501178 to your computer and use it in GitHub Desktop.
Save jameswomack/9501178 to your computer and use it in GitHub Desktop.
Passing a string to `setTimeout` in browsers results in `eval` being used
function logPIAfterDelay() {
setTimeout(function () {
console.log(Math.PI);
}, 1000);
}
function logPIAfterDelayEval() {
setTimeout("console.log(Math.PI)", 1000);
}
function logPIAfterDelayEvalStrict() {
'use strict';
setTimeout("console.log(Math.PI)", 1000);
}
/*
All three of these work in modern browsers (not in node though). Result in Node.js:
timers.js:124
first._onTimeout();
^
TypeError: Property '_onTimeout' of object [object Object] is not a function
at Timer.listOnTimeout (timers.js:124:15)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment