setTimeout not working inside infinite loop

前端 未结 5 1882
花落未央
花落未央 2020-12-21 19:28
    while(true){
        window.setTimeout(function() {
            myMethod()
        }, 15000);
        }
        function myMethod() {
            alert(\"repeat\         


        
5条回答
  •  礼貌的吻别
    2020-12-21 19:33

    but once I run the code my browser hangs

    it will because you are endlessly creating setTimeout() requests by doing while(true) since it will not wait for 15 secs before doing next iteration of while

    if you want to keep alerting something every 15 sec then try

    window.setTimeout( myMethod, 15000);
    function myMethod() 
    {
        alert("repeat");
        window.setTimeout( myMethod, 15000);
    }
    

提交回复
热议问题