I don't know what is the purpose but you would have to interrupt
function count() {
while(keepGoing) {
i = i+1;
}
}
for a while to give a chance for keepGoing to change in some other place that runs meanwhile. Also you never do this:
while(keepGoing) {
i = i+1;
}
You are completely blocking the thread for everything...
You will have to divide your function's work into small pieces and use setTimeout or setInterval to run it in small batches, something like the following, while close to what you may want:
var piece_n=0;
var keepGoing = true;
var interval_id = setInterval(function () {
if(keepGoing){
//do_a_short_piece_of_work(piece_n);
piece_n++;
}else{
clearInterval(interval_id);
}
},500); //ticking every half second
setTimeout(function () { keepGoing = false; }, 10000); //run for a small bit more than 10 to 10.5 seconds + do_a_short_piece_of_work() execution time
If you need exactly 10 seconds without starving the rest you will need to adjust in a series of setTimeout and you will need to know a bit in advance (more than next tick) so you can set the last setTimeout at the exact time (consulting current date and saved initial date).
Everything can be divided into smaller chunks, like instructions for a cpu :)