using setTimeout synchronously in JavaScript

后端 未结 9 1731
无人及你
无人及你 2020-12-02 12:40

I have the following scenario:

setTimeout(\"alert(\'this alert is timedout and should be the first\');\", 5000);
alert(\"this should be the second one\");
         


        
9条回答
  •  执笔经年
    2020-12-02 13:31

    Is the code contained in a function?

    function test() {
        setTimeout(...);     
    
        // code that you cannot modify?
    }
    

    In that case, you could prevent the function from further execution, and then run it again:

    function test(flag) {
    
        if(!flag) {
    
            setTimeout(function() {
    
               alert();
               test(true);
    
            }, 5000);
    
            return;
    
        }
    
        // code that you cannot modify
    
    }
    

提交回复
热议问题