Javascript中的多个计数器for循环 [英] Multiple counters in Javascript for loop

查看:136
本文介绍了Javascript中的多个计数器for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我将我的头发拔出并在调试方面走错了路。有人可以确认此代码将按预期执行。 IE浏览器。动画5 div到不同的位置:

  var i,j,k; (i = 1; j = 0; k = 150; i <= 5; i ++; j + = 30; k- = 30){
$('。spinner #item'+ i).animate({
left:'+ ='+ j,
bottom:'+ ='+ k
},500,function(){
//动画完成。
});
}
});

当我点击 #menuButton 时,发生,我得到这个错误︰


未捕获的SyntaxError:意外的标记;在'for()'行...


解决方案

你有一些分号(i = 1,j = 0,k = 150; i <= 5; i ++, j + = 30,k- = 30){/ * do work * /}

对于([init];},

 中的三个语句 [$] $ {$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ > [inits]  [increment] 您必须使用神奇的,但忘记了,逗号运算符 


Before i tear my hair out and go down the wrong path in terms of debugging. Can someone confirm that this code will do as intended. Ie. animate 5 divs to different positions:

var i, j, k;
$('#menuButton').click(function(){
    for (i=1; j=0; k=150; i<=5; i++; j+=30; k-=30){
        $('.spinner #item' + i).animate({
            left: '+=' + j,
            bottom: '+=' + k
          }, 500, function() {
            // Animation complete.
        });
    }
});

When i click the #menuButton, nothing happens and I get this error:

Uncaught SyntaxError: Unexpected token ; on the 'for()' line...

解决方案

You've got some semicolons where you want commas:

for (i=1, j=0, k=150; i<=5; i++, j+=30, k-=30) { /* do work */ }

You should only have three "statements" inside your for

for( [init]; [test]; [increments]) { [block] }

To do multiple [inits] or [increments] you have to use the sometimes magical, but oft forgotten, comma operator

这篇关于Javascript中的多个计数器for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆