ClearInterval() 不会停止我的旋转功能
我正在使用 jQuery Rotate 插件,将图像旋转 90 度,然后停止旋转。
我的问题是,即使在调用clearInterval()之后,它也不会停止旋转;
$(document).ready(function() {
var finalAngle;
var intval = setInterval(function func()
{
$("#myimg").rotate(1);
if(typeof func.angle == 'undefined' )
{
func.angle = 0;
}
func.angle += 1;
finalAngle = func.angle;
}, 1);
if(finalAngle == 90)
{
clearInterval(intval);
}
});
基本上我所做的就是(静态地)计算角度,一旦达到 90,就调用clearInterval 函数。我必须引入另一个变量来存储计数,以便我可以在 setInterval 函数之外访问它。
I'm using the jQuery Rotate plugin, to animate the rotation of an image 90 degrees, and then stop the rotation.
My problem is that it won't stop rotating, even after calling clearInterval();
$(document).ready(function() {
var finalAngle;
var intval = setInterval(function func()
{
$("#myimg").rotate(1);
if(typeof func.angle == 'undefined' )
{
func.angle = 0;
}
func.angle += 1;
finalAngle = func.angle;
}, 1);
if(finalAngle == 90)
{
clearInterval(intval);
}
});
Basically all I'm doing is (statically) counting the angles, and once it hits 90, call the clearInterval function. I had to introduce another variable to store the count so that I could access it outside the setInterval function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很容易。调用
setInterval
,然后评估finalAngle
的条件,并在一毫秒后调用旋转函数。因此,当finalAngle
为 90 时,永远不会调用clearInterval
。此代码应该适合您:
It's easy.
setInterval
is called then the condition withfinalAngle
is evaluated and after one milisecond your rotating function is called. ThereforeclearInterval
is never called whenfinalAngle
is 90.This code should work for you:
if
语句执行一次。试试这个?虽然我不明白为什么你不能逃脱......
The
if
statement is executed once. Try this?Though I don't see why you can't just get away with...