Skip to content

Instantly share code, notes, and snippets.

@lambder
Created January 11, 2011 11:27
Show Gist options
  • Save lambder/774317 to your computer and use it in GitHub Desktop.
Save lambder/774317 to your computer and use it in GitHub Desktop.
Example of how to utilize setTimeout and accumulation to have non blocking long lasting recurrent calculations
function _factorial(acc, n, callback){
if(n==0){
callback(acc);
}else{
var callback_wrapper = function(result){
callback(result);
};
setTimeout(function(){_factorial(acc * n, n-1, callback_wrapper)}, 10);
}
}
function factorial(n, callback){
_factorial(1, n, callback);
}
factorial(1000, function(result){console.log(result)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment