Created
November 22, 2014 09:04
-
-
Save alessioalex/52d7b1bccd6589569709 to your computer and use it in GitHub Desktop.
heavy-computation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO: in a single threaded JavaScript world (both in the browser and Node) | |
// how would you make the displaySum non-blocking? | |
var displaySum = function outputSumOfEvenNumbers(stopAt) { | |
var sum = 0; | |
for (var i = 0; i < stopAt; i++) { | |
if (i % 2 === 0) { | |
sum += i; | |
} | |
} | |
console.log('sum === %s', sum); | |
clearInterval(interval); | |
}; | |
var interval = setInterval(function() { | |
console.log("I'm alive!!"); | |
}, 200); | |
console.time('sum took'); | |
displaySum(199999999); | |
console.timeEnd('sum took'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the point of running the interval and then do the calculation afterwards?