列表

详情


FED54. 计时器

描述

实现一个打点计时器,要求
1、从 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一个数字,每次数字增幅为 1
2、返回的对象中需要包含一个 cancel 方法,用于停止定时操作
3、第一个数需要立即输出

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 856ms, 内存消耗: 77880KB, 提交时间: 2020-11-23

{"css":"","js":"function count(start, end) {\n    console.log(start++) \n    var timer = setInterval(function(){\n        if(start <= end) {\n            console.log(start++)\n        }else {\n            clearInterval(timer)\n        }\n    },100)\n    return {cancel:function () {\n        clearInterval(timer)\n    }}\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 858ms, 内存消耗: 77772KB, 提交时间: 2020-11-23

{"css":"","js":"function count(start, end) {\n  //立即输出第一个值\n  console.log(start++);\n     var timer = setInterval(function(){\n         if(start <= end){\n             console.log(start++);\n         }else{\n             clearInterval(timer);\n         }\n     },100);\n    //返回一个对象\n     return {\n         cancel : function(){\n             clearInterval(timer);\n         }\n     };\n } ","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 860ms, 内存消耗: 77772KB, 提交时间: 2021-03-15

{"css":"","js":"function count(start, end) {\n    console.log(start++);\n    var timer = setInterval(function(){\n        if(start <= end){\n            console.log(start++)\n        }else{\n            clearInterval(timer)\n        }\n    },100);\n    return {\n        cancel:function(){\n        clearInterval(timer)\n        }\n    }\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 861ms, 内存消耗: 77772KB, 提交时间: 2021-06-26

{"css":"","js":"function count(start, end) {\n    console.log(start);\n    let timer = setInterval(()=>{\n        if(start<end){\n            console.log(++start);\n        }else{\n            clearInterval(timer);\n        }\n    },1000)\n    return {\n        cancel(){\n            clearInterval(timer);\n        }\n    }\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 863ms, 内存消耗: 77792KB, 提交时间: 2021-03-15

{"css":"","js":"function count(start, end) {\n    console.log(start++);\n    let index=start\n    let timer=setInterval(()=>{if(index<=end){console.log(index++)}},100)\n    return {cancel:()=>{clearInterval(timer)}}\n}","html":"","libs":[]}

上一题