I donβt know what the goal is, but you have to interrupt
function count() { while(keepGoing) { i = i+1; } }
for some time, to give a chance for keepGoing to change elsewhere, which runs in the meantime. Also you never do this:
while(keepGoing) { i = i+1; }
You completely block the flow for everything . You will need to divide the function into small pieces and use setTimeout or setInterval to run it in small batches, something like the following, while close to what you might need:
var piece_n=0; var keepGoing = true; var interval_id = setInterval(function () { if(keepGoing){
If you need exactly 10 seconds without starvation, you will need to set up a setTimeout series, and you will need to know a little in advance (more than the next tick) so that you can set the last setTimeout at the exact time (consultation of the current date and the saved start date). Everything can be divided into smaller pieces, for example, instructions for the processor :)
source share