Created
January 11, 2011 11:27
-
-
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
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
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