var arr = [ function() { console.log("A"); }, function() { throw new Error("boom!"); }, function() { console.log("B"); }, function() { console.log("C"); } ];for (var i = 0, ilen = arr.length; i < ilen; i++) { arr[i]();}
oops?
var arr = [ function() { console.log("A"); }, function() { throw new Error("boom!"); }, function() { console.log("B"); }, function() { console.log("C"); } ];for (var i = 0, ilen = arr.length; i < ilen; i++) { window.setTimeout(arr[i], 0);}
more interesting results
var fn = function() { console.log("a"); fn();};fn();
stack overflow?
let’s write own scheduler
var scheduler;(function() { var events = []; scheduler = { add : function(fn, delay) { for (var i = 0, ilen = events.length; i < ilen; i++) { if (events[i].fn === fn) { throw new Error("The event exists in the main event loop"); } } events.push({ fn : fn, delay: delay }); }, run: function() { for (var i = 0, ilen = events.length; i < ilen; i++) { (function(callback, delay) { var fn = function() { callback(); window.setTimeout(fn, delay); }; window.setTimeout(fn, delay); })(events[i].fn, events[i].delay); } } };})();scheduler.add(function() { console.log("A"); }, 500); // it will call a functionevery 500msscheduler.add(function() { console.log("B"); }, 1000); // like above with 1000msdelayscheduler.run();
one loop to handle similar events
var eventLoop;(function() { var events = {}; eventLoop = { add : function(fn, delay) { if (!(delay in events)) { events[delay] = []; } else { for (var i = 0, ilen = events[delay].length; i < ilen; i++) { if (events[delay][i].fn === fn) { throw new Error("The event exists in the main event loop"); } } } events[delay].push(fn); }, run: function() { for (var delay in events) { (function(stack, delay) { var fn = function() { for (var i = 0, ilen = stack.length; i < ilen; i++) { stack[i](); } window.setTimeout(fn, delay); }; window.setTimeout(fn, delay); })(events[delay], delay); } } };})();eventLoop.add(function() { console.log("A"); }, 500);eventLoop.add(function() { console.log("B"); }, 500);eventLoop.run();
var eventLoop;(function() { we don’t want to end up with big array indexes - var events = {}; we use the object instead of array eventLoop = { add : function(fn, delay) { if (!(delay in events)) { we have never added events for this delay so let’s events[delay] = []; make an array to store them } else { for (var i = 0, ilen = events[delay].length; i < ilen; i++) { if (events[delay][i].fn === fn) { throw new Error("The event exists in the main event loop"); } } } events[delay].push(fn); let’s store a single event by that }, run: function() { for (var delay in events) { (function(stack, delay) { var fn = function() { for (var i = 0, ilen = stack.length; i < ilen; i++) { stack[i](); } window.setTimeout(fn, delay); }; window.setTimeout(fn, delay); })(events[delay], delay); } } };})();eventLoop.add(function() { console.log("A"); }, 500);eventLoop.add(function() { console.log("B"); }, 500);eventLoop.run();
yep, you have to implement stop() method
Timers can be useful with AJAX requests
var throttle = function(fn, delay) { var timer = null; return function () { var context = this; var args = arguments; clearTimeout(timer); timer = setTimeout(function () { fn.apply(context, args); }, delay); }; }; $(input.username).keypress(throttle(function (event) { // do the Ajax request }, 250));http://remysharp.com/2010/07/21/throttling-function-calls/
parseInt(„09”) === 0
JS thinks „09” is an octal number because it starts with 0
parseInt(„09”, 10) === 9
However,parseFloat(„09”) === 9
However,parseFloat(„09”) === 9
document.querySelectorAll("div") returns a NodeList not an array
var nodes = document.querySelectorAll("div");nodes = [].slice.apply(nodes);
However:„Whether the slice function can be applied successfully to a host object is implementation-dependent.” - ECMAScript