Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple example demonstrating how setTimeout(fn, 0) can be useful. This gist illustrates the answer given by DVK's on stackoverflow.
<script type="text/javascript" src=""></script>
<button id='do'> Do long calc!</button>
<div id='status'></div>
<div id='result'></div>
$('#do').on('click', function(){
// without set timeout, user will never see "calculating...."
// with set timeout, works as expected
function long(){
var result = 0
for (var i = 0; i<1000; i++){
for (var j = 0; j<1000; j++){
for (var k = 0; k<1000; k++){
result = result + i+j+k
$('#status').text('calclation done') // has to be in here for this example. or else it will ALWAYS run instantly. This is the same as passing it a callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.