本文介绍了jQuery:无限循环不会使浏览器崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有可能创建一个不会导致浏览器崩溃的无限循环,而我正在处理的是画廊类型的东西,当它在屏幕上滚动时会不断跳动.
I was wondering if it was possible to create an infinite loop which does not crash the browser I am working on a gallery type thing which will pulse as it scrolls across the screen.
这是我到目前为止(显然使浏览器崩溃):
This is what I have so far (which obviously crashes the browser):
var i = 0;
while (i < 1){
$('.block').each(function(index) {
$(this).css('left', $(this).position().left - 10)
if (($(this).position().left) < ($(window).width() * 0.4)) {
$(this).html('<p>Test 1</p>');
$(this).animate({
width: "500px",
height: "500px",
}, 500 );
}else if (($(this).position().left) < ($(window).width() * 0.2)) {
$(this).html('<p>Test 1</p>');
$(this).animate({
width: "600px",
height: "600px",
}, 500 );
}
});
}
任何提示都是伟大的!
推荐答案
尝试使用 window.setInterval()像下面的代码(它将在3秒的间隔内执行):
Try to use window.setInterval() like code bellow (it will be execute with interval 3 second):
function LoopForever() {
$('.block').each(function(index) {
$(this).css('left', $(this).position().left - 10)
if (($(this).position().left) < ($(window).width() * 0.4)) {
$(this).html('<p>Test 1</p>');
$(this).animate({
width: "500px",
height: "500px",
}, 500 );
}else if (($(this).position().left) < ($(window).width() * 0.2)) {
$(this).html('<p>Test 1</p>');
$(this).animate({
width: "600px",
height: "600px",
}, 500 );
}
});
}
var interval = self.setInterval(function(){LoopForever()},3000);
//call code bllow to stop interval
//window.clearInterval(interval);
注意:1000 = 1秒;
Note: 1000 = 1 second;
这篇关于jQuery:无限循环不会使浏览器崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!