循环中的参数列表之后的未捕获的SyntaxError:missing)

时间:2015-07-18 00:55:46

标签: javascript

我在第21行的代码中遇到同样的错误。

if(typeof jQuery === undefined){
    throw "jQuery is required for sapphire to work. Didn't you read the     README?";
}
(function ( $ ) {

$.fn.slider = (function(options,images) {
    var settings = $.extend({
      slideCount: 4,
      animationType:"none",
      slideDuration:2000,
      sliderSize:1100,
      looptimes:300000000000000000000000000000000000000000
    },options);
    for(var i = 0; i<options.looptimes; i++){
      var j = 0;
      $("#sapphire-slide").append("<img src='"+images[j]+"'>");
      setTimeout(function() {
        if(j<options.slideCount-1)
          j++;
         }else if(j===options.slideCount-1){
           j=0;
         }
      },options.slideDuration);
    }
);

})( jQuery );

我不确定导致此错误的原因,对我来说它看起来非常精细。谢谢!

2 个答案:

答案 0 :(得分:3)

您在if函数中为setTimeout获得了额外的结束括号:

if (j < options.slideCount - 1)
      j++;
// the errant closing brace is on the next line:
}
else if(j === options.slideCount - 1)
{
   j = 0;
}

或者,正如其他人所提到的,在if添加一个左大括号以构成一个正确的阻止:

if (j < options.slideCount - 1)
{   // you need this opening brace
      j++;
}
else if(j === options.slideCount - 1)
{
   j = 0;
}

答案 1 :(得分:1)

您错过了{上的开头if,并且您错过了}之前的倒数第二行的结束)

(function ($) {

    $.fn.slider = (function (options, images) {
        var settings = $.extend({
            slideCount: 4,
            animationType: "none",
            slideDuration: 2000,
            sliderSize: 1100,
            looptimes: 300000000000000000000000000000000000000000
        }, options);
        for (var i = 0; i < options.looptimes; i++) {
            var j = 0;
            $("#sapphire-slide").append("<img src='" + images[j] + "'>");
            setTimeout(function () {
                if (j < options.slideCount - 1) {  // <--- add this {
                    j++;
                } else if (j === options.slideCount - 1) {
                    j = 0;
                }
            }, options.slideDuration);
        } 
    });    // <--- add a } on this line

})(jQuery);

请注意,修复此类问题只需计算括号和括号,并确保它们是平衡的。