Skip to content

Instantly share code, notes, and snippets.

@alessioalex
Created November 22, 2014 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alessioalex/52d7b1bccd6589569709 to your computer and use it in GitHub Desktop.
Save alessioalex/52d7b1bccd6589569709 to your computer and use it in GitHub Desktop.
heavy-computation.js
// 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');
@alifhaikal88
Copy link

What's the point of running the interval and then do the calculation afterwards?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment