while语句无限执行
while statement executed infinitely
我有一个使用 animation()
的图片 sldieshow 插件,我希望它只在悬停事件上工作,并在 mouseout
上将图像重置为默认位置
只面临两个问题,我无法停止动画,以及浏览器内存占用过多
这是我使用的:
$('.slideshow').hover(function() {
$(this).find('li:first').attr('flag', '');
$(this).slide()
}, function() {
$(this).find('ul').stop(true); //if you keep the mouse a little bit long it wont stop
// and this while statement blocks the browser
while ($('[flag]').index() != 0) {
$(this).slide(1) // the parameter 1 here is to slide only once
}
})
为了全面理解这里是我的 [DEMO]
所以我上面所说的是我实际期望的,但它得到的是堆栈
为什么会这样
注意:我没有使用任何增量,因为幻灯片插件只执行一次,然后在不满足条件时重新执行
您可能想要这样的东西(请填写实际代码):
function changeSlide(ss) {
if ($(ss).hasClass('hover') {
// transition to the next slide
}
setTimeout(function() {changeSlide(ss)}, 5000 /*milliseconds*/);
}
$('.slideshow').on('mouseover',function() {
$(this).addClass('hover');
changeSlide(this);
}).on('mouseout',function() {
$(this).removeClass('hover');
});
我有一个使用 animation()
的图片 sldieshow 插件,我希望它只在悬停事件上工作,并在 mouseout
上将图像重置为默认位置
只面临两个问题,我无法停止动画,以及浏览器内存占用过多
这是我使用的:
$('.slideshow').hover(function() {
$(this).find('li:first').attr('flag', '');
$(this).slide()
}, function() {
$(this).find('ul').stop(true); //if you keep the mouse a little bit long it wont stop
// and this while statement blocks the browser
while ($('[flag]').index() != 0) {
$(this).slide(1) // the parameter 1 here is to slide only once
}
})
为了全面理解这里是我的 [DEMO]
所以我上面所说的是我实际期望的,但它得到的是堆栈
为什么会这样
注意:我没有使用任何增量,因为幻灯片插件只执行一次,然后在不满足条件时重新执行
您可能想要这样的东西(请填写实际代码):
function changeSlide(ss) {
if ($(ss).hasClass('hover') {
// transition to the next slide
}
setTimeout(function() {changeSlide(ss)}, 5000 /*milliseconds*/);
}
$('.slideshow').on('mouseover',function() {
$(this).addClass('hover');
changeSlide(this);
}).on('mouseout',function() {
$(this).removeClass('hover');
});