2012-02-12 61 views
0

相当难住了这一点。JQuery的/的Youtube API - 在模拟DOM太多的递归调用错误点击

我使用YouTube JS API,以确定一首歌曲已经结束。当它结束时,我想调用播放下一首歌曲的方法。

这里是我的事件监听器方法(CoffeeScript的):

window.statechange = (newstate) -> 
    debug = true 
    if debug then console.log 'Player State Change: ' + newState 
    switch newState 
     when 0 #song ended 
      if debug then console.log 'Song Ended' 
     when 1 #song is playing 
      if debug then console.log 'Song Playing' 
      #change the title of the page 
      document.title = $('#currentSongTitle').html() + ' by ' + 
          $('#currentSongArtist').html() 
     when 2 #song is paused 
      if debug then console.log 'Song Paused' 
     when 3 #song is buffering 
      if debug then console.log 'Song Loading' 

的方法正常工作以这种形式。当歌曲结束,Firebug的控制台显示Song Ended

然而,当我修改代码以添加一个模拟点击,将触发下一首歌曲的功能,它给了我一个too much recursion错误和崩溃:

when 0 #song ended 
     if debug then console.log 'Song Ended' 
     $('.next-song').click() 

这里是下一首歌点击时的代码:

​​

就是这样。在它崩溃之前,控制台会说'下一首歌曲被点击',但之后它会崩溃,并给出太多的递归错误。一条线如何导致任何递归?

更加混乱的是,以下方法适用于没有错误。

$(document).on 'click', '#debug-next-song', -> 
    $('.next-song').click() 

编辑:我与确保模拟点击作品的目的,创造了它找出了一切工作正常在Chrome,但在Firefox完全崩溃。你可以在(http://t3k.no/beta)看到错误。在当前歌曲的右侧,单击“下一步”可以正常工作。但是,如果你开始播放歌曲在屏幕的底部,等待它结束,代码$('.next-song').click()被称为和崩溃的Firefox。

+0

点击可能触发另一个'window.statechange'调用。你有没有在任何地方玩的例子? – 2012-02-12 22:37:38

+0

这就是我一开始以为过,但它没有任何意义,我认为这会触发该方法。如果是,控制台应该记录'if debug then console.log'Player State Change:'+ newState'如果状态发生变化。 我上传的破碎版本到我的网站在这里:http://t3k.no/beta(注意,它崩溃我的Firefox当歌曲结束) – CHawk 2012-02-13 02:45:10

+0

更加古怪:现在,我上传到我的网站,我发现它在Chrome但崩溃的Firefox ... – CHawk 2012-02-13 02:47:31

回答

1

我真的不知道为什么发生这种情况,但通过您网站上的源去我注意到你实际上触发回的点击。我不明白为什么这不起作用,但如果你将它们分成两个陈述,它似乎对我有用。

变化:

switch (newState) { 
     case 0: 
     if (debug) console.log('Song Ended'); 
     return $('.next-song').click(); 
// ... 

到:

switch (newState) { 
     case 0: 
     if (debug) console.log('Song Ended'); 
     $('.next-song').click(); 
     return; 
// ... 

由于您使用的CoffeeScript,你可以明确地使用返回关键字。

when 0 #song ended 
    if debug then console.log 'Song Ended' 
    $('.next-song').click() 
    return 
+0

非常感谢Sandro!这实际上是造成这个问题的原因。这让我意识到作为开发人员的一大弱点。我直接跳入Coffeescript而没有JS的强大基础知识,所以我从来没有能够调试编译的JS。我将来确实需要努力......再次感谢!我会在20小时内给你奖赏你的赏金。 – CHawk 2012-02-15 03:14:11