Javascript 通过将变量重新设置为0重复for循环
为什么下面的代码不能再次使var i=0并重新运行循环Javascript 通过将变量重新设置为0重复for循环,javascript,for-loop,Javascript,For Loop,为什么下面的代码不能再次使var i=0并重新运行循环 for (var i = 0; i < menuitems.length; i++){ if (i == menuitems.length){ i = 0; }; $(menuitems[i]).delay(3000*i).queue(function() { $(this).trigger('click'); }); }; (var i=0;i
for (var i = 0; i < menuitems.length; i++){
if (i == menuitems.length){
i = 0;
};
$(menuitems[i]).delay(3000*i).queue(function() {
$(this).trigger('click');
});
};
(var i=0;i
如果(i==menuitems.length){
i=0;
};
$(menuitems[i])。延迟(3000*i)。队列(函数(){
$(this.trigger('click');
});
};
因为“i”将永远不会=
到menuitems.length
(在循环体中;当然,它将在循环之后)。只要“i”不小于menuitems.length
,循环就会终止,因为“i”将永远不会==
到menuitems.length
(在循环体中;当然,它将在循环之后)。只要“i”不小于menuitems.length
,循环就会终止,因为“i”将永远不会==
到menuitems.length
(在循环体中;当然,它将在循环之后)。只要“i”不小于menuitems.length
,循环就会终止,因为“i”将永远不会==
到menuitems.length
(在循环体中;当然,它将在循环之后)。只要“i”不小于menuitems.length
,循环就会终止,因为当i
变为menuitems.length
时,for循环条件(i
)变为false,因此循环停止
请尝试以下方法:
if (i == menuitems.length - 1){
但是,您需要某种方法使循环停止;否则它将继续运行并冻结您的浏览器。因为当
i
变为menuitems.length
时,for循环条件(i
)变为false,因此循环停止
请尝试以下方法:
if (i == menuitems.length - 1){
但是,您需要某种方法使循环停止;否则它将继续运行并冻结您的浏览器。因为当
i
变为menuitems.length
时,for循环条件(i
)变为false,因此循环停止
请尝试以下方法:
if (i == menuitems.length - 1){
但是,您需要某种方法使循环停止;否则它将继续运行并冻结您的浏览器。因为当
i
变为menuitems.length
时,for循环条件(i
)变为false,因此循环停止
请尝试以下方法:
if (i == menuitems.length - 1){
但是,您需要某种方法使循环停止;否则它将继续运行并冻结您的浏览器。这无法工作,因为JavaScript代码是不可中断的。当您在循环中时,将不会执行计时器过期之类的事件,因此外部的任何人都不会为您设置退出条件
for (var i = 0;;i++)
{
i %= menuitems.length; // loops i when it exceeds list length
// the expiration of these delays will not interrupt the loop
$(menuitems[i]).delay(3000*i).queue(function() {
$(this).trigger('click');
});
// if you don't compute a break condition from within the loop, you're toast
if (some_break_condition) break;
}
现在,如果您想要一个滚动条,可以这样做:
var timer;
var period = 1000; // milliseconds
function startScroll ()
{
stopScroll();
timer = window.setInterval(
move_something_around (), // do you scroll here
period);
}
function stopScroll ()
{
if (timer)
{
window.clearInterval(timer);
timer = null;
}
}
// html
<button type="button" onclick="startScroll()">Start</button>
<button type="button" onclick="stopScroll()" >Stop </button>
var定时器;
var期间=1000;//毫秒
函数startScroll()
{
停止滚动();
定时器=window.setInterval(
移动某物(),//是否在此处滚动
期间);
}
函数stopScroll()
{
中频(定时器)
{
窗口清除间隔(计时器);
定时器=空;
}
}
//html
开始
停止
这无法工作,因为JavaScript代码是不可中断的。当您在循环中时,将不会执行计时器过期之类的事件,因此外部的任何人都不会为您设置退出条件
for (var i = 0;;i++)
{
i %= menuitems.length; // loops i when it exceeds list length
// the expiration of these delays will not interrupt the loop
$(menuitems[i]).delay(3000*i).queue(function() {
$(this).trigger('click');
});
// if you don't compute a break condition from within the loop, you're toast
if (some_break_condition) break;
}
现在,如果您想要一个滚动条,可以这样做:
var timer;
var period = 1000; // milliseconds
function startScroll ()
{
stopScroll();
timer = window.setInterval(
move_something_around (), // do you scroll here
period);
}
function stopScroll ()
{
if (timer)
{
window.clearInterval(timer);
timer = null;
}
}
// html
<button type="button" onclick="startScroll()">Start</button>
<button type="button" onclick="stopScroll()" >Stop </button>
var定时器;
var期间=1000;//毫秒
函数startScroll()
{
停止滚动();
定时器=window.setInterval(
移动某物(),//是否在此处滚动
期间);
}
函数stopScroll()
{
中频(定时器)
{
窗口清除间隔(计时器);
定时器=空;
}
}
//html
开始
停止
这无法工作,因为JavaScript代码是不可中断的。当您在循环中时,将不会执行计时器过期之类的事件,因此外部的任何人都不会为您设置退出条件
for (var i = 0;;i++)
{
i %= menuitems.length; // loops i when it exceeds list length
// the expiration of these delays will not interrupt the loop
$(menuitems[i]).delay(3000*i).queue(function() {
$(this).trigger('click');
});
// if you don't compute a break condition from within the loop, you're toast
if (some_break_condition) break;
}
现在,如果您想要一个滚动条,可以这样做:
var timer;
var period = 1000; // milliseconds
function startScroll ()
{
stopScroll();
timer = window.setInterval(
move_something_around (), // do you scroll here
period);
}
function stopScroll ()
{
if (timer)
{
window.clearInterval(timer);
timer = null;
}
}
// html
<button type="button" onclick="startScroll()">Start</button>
<button type="button" onclick="stopScroll()" >Stop </button>
var定时器;
var期间=1000;//毫秒
函数startScroll()
{
停止滚动();
定时器=window.setInterval(
移动某物(),//是否在此处滚动
期间);
}
函数stopScroll()
{
中频(定时器)
{
窗口清除间隔(计时器);
定时器=空;
}
}
//html
开始
停止
这无法工作,因为JavaScript代码是不可中断的。当您在循环中时,将不会执行计时器过期之类的事件,因此外部的任何人都不会为您设置退出条件
for (var i = 0;;i++)
{
i %= menuitems.length; // loops i when it exceeds list length
// the expiration of these delays will not interrupt the loop
$(menuitems[i]).delay(3000*i).queue(function() {
$(this).trigger('click');
});
// if you don't compute a break condition from within the loop, you're toast
if (some_break_condition) break;
}
现在,如果您想要一个滚动条,可以这样做:
var timer;
var period = 1000; // milliseconds
function startScroll ()
{
stopScroll();
timer = window.setInterval(
move_something_around (), // do you scroll here
period);
}
function stopScroll ()
{
if (timer)
{
window.clearInterval(timer);
timer = null;
}
}
// html
<button type="button" onclick="startScroll()">Start</button>
<button type="button" onclick="stopScroll()" >Stop </button>
var定时器;
var期间=1000;//毫秒
函数startScroll()
{
停止滚动();
定时器=window.setInterval(
移动某物(),//是否在此处滚动
期间);
}
函数stopScroll()
{
中频(定时器)
{
窗口清除间隔(计时器);
定时器=空;
}
}
//html
开始
停止
我最终用以下代码绕过了这个问题:
$('#scroll').click(function(){
setInterval(function(){
var u = 0;
document.getElementById('maps').setAttribute('src',myUrlArray[u]);
if(u < myUrlArray.length){
++u;
}else{
u = 0;
}
},3000);
});
$('#滚动')。单击(函数(){
setInterval(函数(){
var u=0;
document.getElementById('maps').setAttribute('src',myUrlArray[u]);
if(u
我的问题最终与此非常相似:
我最终用以下代码绕过了这个问题:
$('#scroll').click(function(){
setInterval(function(){
var u = 0;
document.getElementById('maps').setAttribute('src',myUrlArray[u]);
if(u < myUrlArray.length){
++u;
}else{
u = 0;
}
},3000);
});
$('#滚动')。单击(函数(){
setInterval(函数(){
var u=0;
document.getElementById('maps').setAttribute('src',myUrlArray[u]);
if(u
我的问题最终与此非常相似:
我最终用以下代码绕过了这个问题:
$('#scroll').click(function(){
setInterval(function(){
var u = 0;
document.getElementById('maps').setAttribute('src',myUrlArray[u]);
if(u < myUrlArray.length){
++u;
}else{
u = 0;
}
},3000);
});
$('#滚动')。单击(函数(){
setInterval(函数(){
var u=0;
document.getElementById('maps').s