basic scheduling with settimeout & setinterval

14
Basic Scheduling with setTimeout & setInterval JavaScript Method of the Month May, 2011 Kevin Munc @muncman Thursday, May 19, 2011

Upload: kevin-munc

Post on 10-May-2015

2.230 views

Category:

Technology


3 download

DESCRIPTION

Method of the Month for the May meeting of the Columbus JavaScript Users Group (CBUSJS).

TRANSCRIPT

Page 1: Basic Scheduling with setTimeout & setInterval

Basic Scheduling with setTimeout & setInterval

JavaScript Method of the MonthMay, 2011Kevin Munc@muncman

Thursday, May 19, 2011

Page 2: Basic Scheduling with setTimeout & setInterval

Do it later setTimeout

Thursday, May 19, 2011

Page 3: Basic Scheduling with setTimeout & setInterval

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

Page 4: Basic Scheduling with setTimeout & setInterval

setTimeout(function() { console.log('Egg:\t\t\t%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

Page 5: Basic Scheduling with setTimeout & setInterval

var myFunc = function() { console.log('CoffeeScript is slick.'); };undefinedsetTimeout('myFunc()', 250);54CoffeeScript is slick.setTimeout(myFunc, 250);55CoffeeScript is slick.

Thursday, May 19, 2011

Page 6: Basic Scheduling with setTimeout & setInterval

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

Page 7: Basic Scheduling with setTimeout & setInterval

Do it periodically setInterval

Thursday, May 19, 2011

Page 8: Basic Scheduling with setTimeout & setInterval

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

Page 9: Basic Scheduling with setTimeout & setInterval

A potential use of setInterval. The best use...?

var blinker = setInterval("$('#siteLogo').toggle()", 500);

if (youThinkBlinkSux) { clearInterval(blinker); }

Thursday, May 19, 2011

Page 10: Basic Scheduling with setTimeout & setInterval

var funcMaker = function() { return function() { console.log('getting funcy at %s', new Date()); } };undefinedsetInterval(funcMaker(), 500);124getting 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

Page 11: Basic Scheduling with setTimeout & setInterval

Handy? Yes. But use with caution.

eval is evilThe 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

Page 12: Basic Scheduling with setTimeout & setInterval

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

Page 13: Basic Scheduling with setTimeout & setInterval

Passing arguments

// Instead of awkwardly concatenating strings, // use an anonymous function.var levar = 'Geordi la Burton';undefinedvar logIt = function(logThis) { console.log(logThis); }undefinedsetTimeout(function() { logIt(levar); }, 500);27Geordi la Burton

Thursday, May 19, 2011

Page 14: Basic Scheduling with setTimeout & setInterval

Questions?

• http://www.flickr.com/photos/olemartin/4074077969/

• http://www.flickr.com/photos/freefoto/5089754554/

• http://www.flickr.com/photos/grufnik/4434081102/

• http://www.flickr.com/photos/simpologist/16734948/

• http://www.flickr.com/photos/bluedelliquanti/3395618720/

• http://blinktag.com/origins-of-the-blinktag/

Photo Credits

Prague Orloj Astronomical Clock

• http://en.wikipedia.org/wiki/Prague_Astronomical_Clock

Thursday, May 19, 2011