setTimeout() method inside a while loop

前端 未结 6 1939
余生分开走
余生分开走 2020-12-09 19:54

I have read the relevant pages on w3schools and other similar questions here but cannot seem to understand what\'s wrong about the following bit :

var myfunc         


        
6条回答
  •  借酒劲吻你
    2020-12-09 20:12

    the while method runs quickly and all timeout almost gets executed after first second.. what you can do is

    1. Instead of while call $timeout from myfunc03 for next value
    2. inside your while call timeout with increasing seconds like i*1000

    Also, as others pointed out you can't call functions with params like that from setTimeout use anonymous function like

    ...
    while (i<100) {
      setTimeout(
         function(i){
            myfunc03(i);
         }, i*1000);
       i++;
    }
    ...
    

    for that

提交回复
热议问题