2012-09-11 89 views
0

我无法发现为什么浏览器挂起并被第一个for循环在这个函数中捕获,arguments.length也不会打印console.log。为什么浏览器卡在这个JavaScript循环中?

这是通过在ColorPicker自己的ID则变量其他属性点击功能改变:

$('#colorSelector3').click(function(){ 
colorPickDynamic('#colorSelector3','h1','color'); 
}); 

这里就是浏览器的第一个挂起循环功能:

function colorPickDynamic(cp){ 
    var i, 
     j, 
     x, 
     tag = [], 
     colorProperty = []; 

    for (x=0, i = 1, j = 2; x <= arguments.length; i+=2, j+=2, x++) { 

     tag[x]=arguments[i]; 
     colorProperty[x]=arguments[j]; 

     console.log(arguments.length); 
     console.log(colorProperty[x]); 
    } 

    $(cp).ColorPicker({ 
     color: '#0000ff', 
     onShow: function (colpkr) { 
      $(colpkr).fadeIn(500); 
      return false; 
     }, 
     onHide: function (colpkr) { 
      $(colpkr).fadeOut(500); 
      return false; 
     }, 
     onChange: function (hsb, hex, rgb) { 
      for (j = 2; j < arguments.length; j+=1) { 
       $(tag[0]).css(colorProperty[0], '#' + hex); 
      } 
      $(cp + ' div').css('backgroundColor', '#' + hex); 
     } 
    }); 
} 

任何帮助将是惊人的!谢谢

+0

在jsfiddle.net上显示演示plz – IProblemFactory

+1

在“循环”消失之前,您可以从中删除多少代码? – Deestan

+1

您可能希望在脚本调试器中的第一个循环“body”中放置一个断点,以便您可以看到“正在发生的内部”是什么...... – benqus

回答

3

你确定它挂起(例如无限循环)吗?而不仅仅是简单的崩溃?

因为您正在访问arguments[i],其中参数从1开始,每次迭代时递增2。如果参数包含零个或一个元素,它将在第一次迭代时崩溃,尝试在任何记录完成之前访问arguments[1]

您是否打算使用arguments[x]

+0

yes即使更改console.log(colorProperty [x]);高达20多万,然后铬崩溃? – Joe

+0

@Joe:你的'colorPickDynamic'怎么被调用?什么*是'arguments.length'的值? –

+0

对deestans检查后感到抱歉,我看到它使用旧的缓存.js文件,我希望循环解决! – Joe

相关问题