【发布时间】:2015-05-17 11:38:10
【问题描述】:
有人能解释一下为什么下面的代码在前几次重复中表现正确,然后加速到疯狂的速度吗?搜索了一下,发现应该在setInterval之前先clearInterval,但是没有区别。
var begin = window.onload = function() {
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = canvas.width = 600,
height = canvas.height = 600;
var stripWidth = Math.floor(Math.random() * (50 - 5 + 1) + 5);
for (i = 0; i <= (width / stripWidth); i++) {
context.strokeRect(stripWidth * i, stripWidth * i, width - (2 * i * stripWidth), width - (2 * i * stripWidth));
}
clearInterval(begin);
setInterval(begin, 1000);
};
【问题讨论】:
标签: javascript google-chrome canvas setinterval