Javascript setInterval works but not infinite loop

前端 未结 2 1284
情歌与酒
情歌与酒 2021-01-25 08:41

I don\'t understand why the infinite loop does not work but uncommenting the function call works.




        
2条回答
  •  借酒劲吻你
    2021-01-25 08:56

    JavaScript-in-a-browser is a part of a larger construction. You need to align to the rules of this construction and don't hurt it. One of the rule is that your JavaScript must run quick and exit then. Exit means that control gets back to the framework, which does all the job, repaint the screen etc.

    Try to hide and show something many times:

    for (n = 0; n < 200; n++) {
      $("#test").hide();
      $("#test").show();
    }
    

    When this code runs, you won't see any flickering, you will see that the last command will have effect.

    Have to say, it's not easy to organize code that way, if you want to make a cycle which paints nice anims on a canvas, you have to do it without while.

提交回复
热议问题