无法确定导致意外令牌错误的原因

时间:2013-06-11 14:30:27

标签: javascript jquery debugging

我在控制台中收到此错误

SyntaxError: Unexpected token '(' ... line 49

这是有问题的一行:

 trackEnded: function() {

这是整个代码,我只是不明白我为什么会收到这个错误?

// Initialize audio js
      $(document).ready(function($) {


              // Setup the player to autoplay the next track
              trackEnded: function() {
                  var next = $('.main article.playing').next();
                  if (!next.length) next = $('.main article').first();
                  next.addClass('playing').siblings().removeClass('playing');
                  audio.load($('.article-play', next).attr('data-src'));
                  audio.play();
                  $('.article-play', next).removeAttr('data-playa').attr("data-pausea", '5');
                  var nextPlay = $('.article-play', next);
                  $('.article-play').not(nextPlay).removeAttr('data-pausea').attr("data-playa", '4');
              }


          //Synchronize Main player with articles
          $(".audiojs .play-pause").click(function() {
              var element = $('.playing .article-play')
              var play = element.attr('data-playa')
              var pause = element.attr('data-pausea')
              if (play == '4') {
                  element.removeAttr('data-playa').attr("data-pausea", '5');
              } else if (pause == '5') {
                  element.removeAttr('data-pausea').attr("data-playa", '4');
              }
          })

          // Load in a track on click
          $('.article-play').click(function(e) {
              e.preventDefault();
              if ($(this).closest('article').hasClass('playing')) {
                  var play = $(this).attr('data-playa')
                  var pause = $(this).attr('data-pausea')
                  if (play == '4') {
                      $(this).removeAttr('data-playa').attr("data-pausea", '5');
                      $('.article-play').not($(this)).removeAttr('data-pausea').attr("data-playa", '4');
                      audio.play();
                  } else if (pause == '5') {
                      $(this).removeAttr('data-pausea').attr("data-playa", '4');
                      audio.pause();
                  }
              } else {
                  $(this).closest('article').addClass('playing').siblings().removeClass('playing');
                  audio.load($(this).attr('data-src'));
                  audio.play();
                  $(this).removeAttr('data-playa').attr("data-pausea", '5');
                  $('.article-play').not($(this)).removeAttr('data-pausea').attr("data-playa", '4');
              }
          });

          // Keyboard shortcuts
          $(document).keydown(function(e) {
              var unicode = e.charCode ? e.charCode : e.keyCode;
              // right arrow
              if (unicode == 39) {
                  var next = $('.main article.playing').next();
                  if (!next.length) next = $('.main article').first();
                  next.find('.article-play').click();
                  // back arrow
              } else if (unicode == 37) {
                  var prev = $('.main article.playing').prev();
                  if (!prev.length) prev = $('.main article').last();
                  prev.find('.article-play').click();
                  // spacebar
              } else if (unicode == 32 && document.activeElement.nodeName !== 'INPUT') {
                  audio.playPause();
                  var element = $('.playing .article-play')
                  var play = element.attr('data-playa')
                  var pause = element.attr('data-pausea')
                  if (play == '4') {
                      element.removeAttr('data-playa').attr("data-pausea", '5');
                  } else if (pause == '5') {
                      element.removeAttr('data-pausea').attr("data-playa", '4');
                  }
              }
          });
      });

3 个答案:

答案 0 :(得分:2)

您正在尝试在函数范围内编写对象synax,这是不正确的语法。

应该是:

function trackEnded() {
    // rest or your code

答案 1 :(得分:1)

问题在于

$(document).ready(function($) {
    // Setup the player to autoplay the next track
    trackEnded: function() {
        ...
    }
    ...
)};

语法错误

您可能希望这样做:

$(document).ready(function($) {
    // Setup the player to autoplay the next track
    function trackEnded() {
        ...
    }
    ...
)};

另外,查看您的制表和评论,您的预期代码似乎是:

$(document).ready(function($) {
    // Setup the player to autoplay the next track
    $(element).AudioPlugin({
        trackEnded: function() {
            ...
        }
    });
    ...
)};

不同之处在于,对于假定的AudioPlugin,我传递的是一个初始化/选项对象,它定义了trackEnded上的函数回调。

答案 2 :(得分:0)

以下是代码的简化,仍然可以重现问题:

function(){
    trackEnded: false
}

总结一下:您正在尝试定义一个对象键,但您没有对象文字。