倒计时有三种写法

来源:互联网 发布:黑暗之魂2原罪学者优化 编辑:程序博客网 时间:2024/06/05 09:50

1. 你知道定时器有几种吗

小陈:”三种。一者曰setTimeout,再者曰setInterval,三者曰questAnimationFrame”

2. setTimeout

var a = 10;var date = new Date();setTimeout(function () {    console.log(a--);    if (a>=0) {        setTimeout(arguments.callee, 1000);    }}, 1000);

3. setInterval

var a=10;var t = setInterval(function () {  console.log(a--);  if (a<0) {    clearInterval(t);  }}, 1000)

4. questAnimationFrame

var a=10;var date = new Date();requestAnimationFrame(function () {    if(new Date()-date<1000) {        requestAnimationFrame(arguments.callee);    } else {        if (a>=0) {            console.log(a--);            date = new Date();            requestAnimationFrame(arguments.callee);        }    }})
0 0