How to use setInterval and clearInterval?

前端 未结 5 912
盖世英雄少女心
盖世英雄少女心 2020-11-22 03:58
function doKeyDown(event) {
    switch (event.keyCode) {
    case 32:
        /* Space bar was pressed */
        if (x == 4) {
            setInterval(drawAll, 20);         


        
5条回答
  •  [愿得一人]
    2020-11-22 04:41

    Side note – if you want to use separate functions to set & clear interval, the interval variable have to be accessible for all of them, in 'relative global', or 'one level up' scope:

    var interval = null;    
    
    function startStuff(func, time) {
        interval = setInterval(func, time);
    }
    
    function stopStuff() {
        clearInterval(interval);
    }
    

提交回复
热议问题