3. setTimeout
• setTimeout(expression, millis)
• on the window object
• returns a numeric ID for the
timeout
• companion clearTimeout(id)
function
• does not block execution
• expressions can get tricky
Thursday, May 19, 2011
4. setTimeout(function() {
console.log('Egg:ttt%s', new Date());
}, 1500);
console.log('Chicken:t%s', new Date());
Chicken:! Wed May 18 2011 09:55:43 GMT-0400 (EDT)
Egg:! ! ! Wed May 18 2011 09:55:45 GMT-0400 (EDT)
Thursday, May 19, 2011
5. var myFunc = function() {
console.log('CoffeeScript is slick.');
};
undefined
setTimeout('myFunc()', 250);
54
CoffeeScript is slick.
setTimeout(myFunc, 250);
55
CoffeeScript is slick.
Thursday, May 19, 2011
6. var myFunc = function() {
console.log('Node is okay, I guess.');
};
var myFuncTimeout = setTimeout(myFunc, 5000);
clearTimeout(myFuncTimeout);
// myFunc is never fired.
Thursday, May 19, 2011
8. setInterval
• setInterval(expression, millis)
• on the window object
• returns a numeric ID for the
interval
• companion clearInterval(id)
function
• does not block execution
• expressions can get tricky
Thursday, May 19, 2011
9. A potential use of setInterval. The best use...?
var blinker = setInterval("$('#siteLogo').toggle()", 500);
if (youThinkBlinkSux) { clearInterval(blinker); }
Thursday, May 19, 2011
10. var funcMaker = function() {
return function() {
console.log('getting funcy at %s', new Date());
}
};
undefined
setInterval(funcMaker(), 500);
124
getting funcy at Wed May 18 2011 14:51:30 GMT-0400 (EDT)
getting funcy at Wed May 18 2011 14:51:31 GMT-0400 (EDT)
getting funcy at Wed May 18 2011 14:51:31 GMT-0400 (EDT)
getting funcy at Wed May 18 2011 14:51:32 GMT-0400 (EDT)
getting funcy at Wed May 18 2011 14:51:32 GMT-0400 (EDT)
getting funcy at Wed May 18 2011 14:51:33 GMT-0400 (EDT)
getting funcy at Wed May 18 2011 14:51:33 GMT-0400 (EDT)
getting funcy at Wed May 18 2011 14:51:34 GMT-0400 (EDT)
clearInterval(124);
undefined
Thursday, May 19, 2011
11. Handy? Yes. But use with caution.
eval is evil
The eval function (and its relatives, Function, setTimeout,
and setInterval) provide access to the JavaScript compiler. This
is sometimes necessary, but in most cases it indicates the presence of
extremely bad coding. The eval function is the most misused feature
of JavaScript.
- quoted from http://www.jslint.com/lint.html
Thursday, May 19, 2011
12. Better to pass functions, not strings.
Pass functions, not strings, to setTimeout() and
setInterval()
The setTimeout() and setInterval() methods are very closely
related to eval. If they are passed a string, then after the specified
delay, that string will be evaluated in exactly the same way as with
eval, including the associated performance impact.
These methods can, however, accept a function as the first parameter,
instead of a string. This function will be run after the same delay, but can
be interpreted and optimized during compilation, with improved
performance as a result.
- quoted from http://dev.opera.com/articles/view/efficient-javascript/?page=2
Thursday, May 19, 2011
13. Passing arguments
// Instead of awkwardly concatenating strings,
// use an anonymous function.
var levar = 'Geordi la Burton';
undefined
var logIt = function(logThis) { console.log(logThis); }
undefined
setTimeout(function() { logIt(levar); }, 500);
27
Geordi la Burton
Thursday, May 19, 2011