【发布时间】:2013-05-27 04:46:01
【问题描述】:
我创建的倒数计时器不起作用。 有趣的是,如果我使用 console.log 打印 count 的值——从 3 开始——打印出类似 -3498 的内容,即使我只打开了大约 15 秒,所以设置的时间间隔肯定有问题代码。显示值(如果count大于0),但是设置间隔变化太快。
这是代码。
function countdown(){
window_width=window.innerWidth-70;
window_height=window.innerHeight-150;
canvas = document.getElementById("gameCanvas");
ctx=canvas.getContext("2d");
canvas.width = window_width;
canvas.height=window_height;
if(count>0){
ctx.font = '40pt Calibri';
ctx.fillStyle = "white";
ctx.fillText(count, window_width/3, window_height/2);
}
else if(count===0){
ctx.fillText("Go!", window_width/3, window_height/2);
}
else{
return;
}
setInterval(function(){count=count-1},1000);
requestAnimationFrame(countdown);
}
任何帮助将不胜感激。
【问题讨论】:
-
看起来
count可能是一个全局变量,如果是这样,那就是你的问题所在。你的 setInterval 函数很好。 Here's a fiddle
标签: javascript html canvas html5-canvas setinterval