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);
}
}
})