如果在 10 秒之前给出答案,我需要一个停止功能。我尝试了一些想法,但找不到解决方案
这是更新的 jsFiddle和我的代码。有人可以帮忙吗?
HTML
<canvas id="timer" width="48" height="48"></canvas>
<span id="num"></span>
<br /><br />
<a href="javascript:istop();">stop</a>
JAVASCRIPT
function sCounter(){
canvas = document.getElementById('timer');
num = document.getElementById('num');
ctx = canvas.getContext('2d');
fps= 40;
var total = fps*10; //*second
for(var i=total;i>=0;i--) {
var delayed = (function(){
var step = 1-i/total;
var left = Math.ceil(i/fps);
return function() {
num.innerHTML=left;
draw_next(step);
if(left===0){ //if 0
//etc...
}
};
})();
var timer = setTimeout(delayed,-1000/fps*(i-total));
}
}
function draw_next(step) {
ctx.clearRect(0,0,48,48);
ctx.beginPath();
ctx.arc(24,24,20,Math.PI * (-0.5 + 0),Math.PI * (-0.5 + step*2),false);
ctx.lineWidth = 8;
ctx.strokeStyle = "#dedede";
ctx.stroke();
}
function istop(e) {
e.preventDefault();
var timer = null;
clearTimeout(timer);
return false;
}
sCounter();