| Asynchronous Code In JavaScript |
| Written by Ian Elliot | ||||||||
| Monday, 15 May 2017 | ||||||||
Page 4 of 4
Problems with TimeoutsThere are some problems that you might encounter using setTimeout. The most common is that you do not get a zero timeout in practice. The obvious reason is that the function is not called until the code that used the setTimeout finishes. There also might be other events that need to be processed before the function is called. In addition different browsers set minimum times and 4ms is the shortest delay specified by HTML5. Often this makes no difference, but if you need the maximum efficiency then you need to take a different approach to implementing an asynchronous function, see Chapter 6. You can also pass parameters to the delayed function but browsers differ in how they handle this. In most cases it is simpler to use closure to provide parameters. Also notice that when the function is called this will be the global window object, rather than what it was when you invoked setTimeout. This is generally only a problem if you try to call a function as an object method. Summary
More InformationJust jQuery
|
JavaScript Canvas - WebGL A 3D Cube It's a right of passage - you have to draw a 3D cube to prove you can! It also needs a deep understanding of WebGL. This extract from Ian Elliot's book on JavaScript Graphics looks at how to use [ ... ] |
JavaScript Data Structures - A Lisp-Like List JavaScript lets you do so much with so little as we show here by implementing a Lisp-like list data structure. |
Other Articles
|
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info



