Javascript setInterval function to clear itself?

前端 未结 5 911
离开以前
离开以前 2020-12-23 19:17
myInterval = setInterval(function(){
     MyFunction();
},50);

function MyFunction()
{
    //Can I call clearInterval(myInterval); in here?
}

The

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 19:41

    You can do it by using a trick with window.setTimeout

    var Interval = function () {
        if (condition) {
            //do Stuff
        }
        else {
            window.setTimeout(Interval, 20);
        };
    };
    window.setTimeout(Interval, 20);
    

提交回复
热议问题