Control a loop --> stopping it for a certain interval of time

时光总嘲笑我的痴心妄想 提交于 2019-12-25 03:06:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!