Uncaught SyntaxError: missing)在循环参数列表后

Uncaught SyntaxError: missing ) after argument list in loop

本文关键字:循环 参数 列表 SyntaxError missing Uncaught      更新时间:2023-09-26

我的代码在第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 );

我不确定是什么导致这个错误,它看起来像完美的语法对我来说。谢谢!

在传递给setTimeout的函数中为if添加了一个额外的右括号:

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;
}

您缺少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);

请注意,修复这种事情只是计数括号和圆括号的问题,并确保它们是平衡的。