4 ms·
how do you delay a function to the next tick in javascript?
by jay_kyburz 15y ago
how do you delay a function to the next tick in javascript?
- andrewem 15y agoThat phrasing isn't very precise, but I presume they mean to use setTimeout and pass a timeout of 0 [0]. That link has some discussion of this use and suggests using window.postMessage instead, at least on "modern browsers", for a suitable definition of modern. [0] https://developer.mozilla.org/en/DOM/window.setTimeout https://developer.mozilla.org/en/DOM/window.setTimeout
- masylum 15y agosetTimeout(fn, 0);
- djblaze 15y agoIm assuming we are talking client-side and 'next tick' means when there is a millisecond of free time. Just use: setTimeout(function, 0) Technically supplying '0' as the milliseconds is bad in old browsers, so you might want to say 10...but I've used '0' a lot without fail.