how to make a sleep in javascript?

后端 未结 9 2055
半阙折子戏
半阙折子戏 2020-12-20 23:00

Does anyone know how can I make a sleep in javascript before next line been read by the system?

example:

    1 var chkResult = Validation();
    2 /         


        
9条回答
  •  一个人的身影
    2020-12-20 23:38

    Javascript has to share the thread with the browser, so the kind of pause you want would look very much like the browser was frozen for 10 seconds, were it possible. Functions such as "alert" and "prompt" do this. For this, they are evil, as a single alert in a loop can bring a whole browser to its knees, and the only way out is to force quit it.

    The way around this problem is the event based paradigm. Javascript has first class functions, so they can be passed around as values for "callbacks" that you can assign to certain events.

    setTimeout is one such event. Others have posted code, but in general, you'll want to just assign your functions to certain events, then get out of the way of the browser, so it can go about its browsery business. The browser will let you know when something has happened, and you can do something appropriate in response, quickly, then get out of the way again. In this way, the browser can maintain the appearance of being responsive.

    if you want to know more, have a look at this question: How is GUI and Game Program Flow compared to Web programs

提交回复
热议问题