setTimeout() method inside a while loop

前端 未结 6 1928
余生分开走
余生分开走 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:02

    Yes. There are 2 problems in your code:

    1. The setTimeout function accept a function as the first argument, but in your code, myfunc03(i) returns nothing
    2. The while loop won't meet you needs, instead, you have to use recursive function. Since the second function should be invoked after the first timeout is fired.

    Sample code:

    var myfunc03 = function (i) {
      setTimeout(function() {
        document.getElementById('d01').innerHTML += 100-i+"
    "; if (i < 100) { i++; myfunc03(i); } }, 1000); }; var myFunc01 = function() { myfunc03(0); }; myFunc01();

提交回复
热议问题