我想以一定的间隔在画布上绘图,但 setTimeout 不起作用
这是代码中重要的部分:
function drawCircle(i, color1, color2) {
var ctx = canvas.getContext("2d");
if (i % 2 == 1) {
ctx.fillStyle = color1;
}
else {
ctx.fillStyle = color2;
}
ctx.beginPath();
ctx.arc(110, 270, 10, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
}
for (var i = 0; i < 10; i++) {
setTimeout(drawCircle(i, color2, color5), 4000);
}
请注意,这只是我在更大的项目中使用与此类似的代码之前想要尝试的一个片段。这无法正常工作,图像仅绘制一次,也就是说,我只看到最后绘制的圆圈。我已经用谷歌搜索了这个,但到目前为止没有任何帮助我。
Here is the part of the code that matters:
function drawCircle(i, color1, color2) {
var ctx = canvas.getContext("2d");
if (i % 2 == 1) {
ctx.fillStyle = color1;
}
else {
ctx.fillStyle = color2;
}
ctx.beginPath();
ctx.arc(110, 270, 10, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
}
for (var i = 0; i < 10; i++) {
setTimeout(drawCircle(i, color2, color5), 4000);
}
Note that this is just a snippet I wanted to try out before I use code similar to this on a larger project. This isn't working properly, the image is only drawn once, that is, I only see the last circle that is drawn. I have googled this to death but nothing has helped me so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您要使用的是
setInterval
。setTimeout
仅触发该事件一次。他们有相同的参数列表。另外,我认为您使用
setTimeout
的方式不正确。按照您现在编写的方式,该函数在实际将任何内容传递给setTimeout
/setInterval
之前被触发。所以你应该写:or:
和 NOT:
EDIT
你在 JavaScript 中没有任何阻塞例程。它是一种单线程、事件驱动的语言。如果您调用类似
setInterval
的方法,它会立即成功。仅在 4 秒左右后,您的回调函数才会被调用。然而与此同时,JS 将忙于做各种不同的事情 - 例如对用户生成的其他事件做出反应。您想要做的是调用setTimeout
一次,然后在回调函数内部,在返回之前使用相同的函数和不同的i
参数集再次调用它。沿着这些思路:What you want to use is
setInterval
.setTimeout
just fires the event once. They have the same argument list.Also I don't think the way you use
setTimeout
is correct. The way you've written it now, the function is fired before actually passing anything tosetTimeout
/setInterval
. So you should write:or:
and NOT:
EDIT
You don't have any blocking routines in JavaScript. It's a single threaded, event driven language. If you call something like
setInterval
it succeeds immediately. Only after 4 seconds or so your callback function will be invoked. However in the meantime JS will be busy doing all sort of different stuff - like for example reacting to other events generated by the user. What you want to do is to callsetTimeout
once and then inside the callback function, just before returning invoke it again with the same function and a different set of arguments ofi
. Something along these lines:实际上有一些因素会导致代码行为不当,而这些因素都与循环的编写方式有关。第一个问题是对 setTimeout 的调用,主要是您没有将函数对象作为第一个参数传递给它,大多数人会通过执行以下操作来解决此问题:
这将导致 setTimeout 按您的预期延迟,但是它将最终以 10 作为第一个参数调用 drawCircle 10 次,这是因为每次调用都使用对“i”变量的相同引用。在调用之前,不会检索 i 的值...当 i 已经是 10 时,循环完成后 4 秒左右。
为了解决这个问题,您还需要一个函数:
getDrawer (一个可怕的名字,请不要使用't use it) 函数会立即被调用,因此 i 的值会被立即访问并被 getDrawer 返回的匿名函数记住。 setTimeout 将调用该匿名函数。
There are actually a couple of things that are causing your code to misbehave, and they're all in how your loop is written. The first problem is the call to setTimeout, mainly that you're not passing a function object to it as the first argument, and most people would fix that by doing something like this:
That will cause the setTimeout to delay as you expected, but it will end up calling drawCircle with 10 as the first argument 10 times, and that is because of every call is using the same reference to the "i" variable. The value for i isn't retrieved until the call is made...4 seconds or so after the loop completed when i is already 10.
To get around that, you need one more function:
The getDrawer (a horrible name, please don't use it) function is called immediately, so the value of i is accessed immediately and remembered by the anonymous function that getDrawer returns. That anonymous function is what will be called by setTimeout.
这里的问题是您连续多次调用 setTimeout,而不是从回调中再次调用它。尝试将对
setTimeout
的调用移至drawCircle
函数的末尾以查看动画。The issue here is that you're calling setTimeout several times in a row, rather than calling it again from the callback. Try moving the call to
setTimeout
to the end of thedrawCircle
function to see the animation.