js算法之定时器

start=1 end=5
思路:先输出1,因为已经执行一次了,所以只需要执行4次循环就行了。
定时器核心:要结束定时器的话,毫秒之后就输出才能结束定时器.

function count(start, end) {
    console.log(start);
    var a=setInterval(function()
    {
        if(start<end)
            {
                console.log(++start);
            }
       
    },100);
    return {
        cancel:function()
        {
            clearInterval(a);
        }
    };
}

在这里插入图片描述

posted @ 2021-03-28 15:11  贵哥的编程之路  阅读(16)  评论(0)    收藏  举报