问题
guys I am trying to do something very interesting! I want to stop a loop for an interval of time, then display a message during that time and then let the loop continue. And of course in the next iteration it should stop again at the same place for some time and display the message again. Here is my code but the problem is that the elements are sorted too fast (the loop doesn't actually stop) and therefore the messages are not is dispayed as I wish. I wounder if there is something like the sleep function in Javascript.
for(var i=0; i<n-1; ++i)
{
r++;
var index = i; // markirame na4alna strelka ^
document.getElementById("demoto5").innerHTML = "^";
(function(i) {
setTimeout(function() {
for(var j=i+1; j<n; ++j)
{
// markirame sys strelki vseki element
if(arr[index] > arr[j])
{
index = j;
}
}
if(i != index)
{
var temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
console.log(i);
}, i * 1000);
})(i);
}
来源:https://stackoverflow.com/questions/23322530/control-a-loop-stopping-it-for-a-certain-interval-of-time