 <p>The while loop is synchronous. You'll need to use an asynchronous loop. You have a couple options for this:</p> <ol> <li>Use a setTimeout and put your while loop in that and use a callback when it gets finished</li> <li>Use a setInterval. </li> </ol> <hr> <pre><code>var loop = function(f, callback){ setTimeout(function(){ var count=0; while(f&gt;10){ count++; } callback(count); }, 0); }; </code></pre> <p>or</p> <pre><code>var loop = function(f, callback){ var count = 0; var si = setInterval(function(){ count++ if(f&lt;=10){ clearInterval(si); callback(count); } }, 1000); }; </code></pre> <hr> <p>Using these functions:</p> <pre><code>loop(frequencyVariable, function(count){ //callback has been called successfully and the loop has ended. //do stuff here }); </code></pre>