2011-04-29 49 views
1

我想使这个代码运行20秒后的setTimeout:SetTimeout如何做到这一点?

<script type="text/javascript"> 
    soundcloud.addEventListener('onPlayerReady', function(player, data) { 
    player.api_play(); 
    }); 
</script> 

代码正常单独,但我不知道如何来延迟它。我该怎么做?

感谢

回答

2
setTimeout(function() { 

    soundcloud.addEventListener('onPlayerReady', function(player, data) { 
    player.api_play(); 
    }); 

}, 20000); 
1

这将延迟player.api_play的执行()onPlayerReady事件。 @alex的代码将延迟注册onPlayerReady事件的处理程序。

<script type="text/javascript"> 
    soundcloud.addEventListener('onPlayerReady', function(player, data) { 
    setTimeout(function(player) { 
     player.api_play(); 
    }, 2000); 
    }); 
</script> 
+0

这可能是OP想要的,我不认为他们具体询问他们想要什么。另外,根据[MDC](https://developer.mozilla.org/en/window.setTimeout)*请注意,在第一种语法中向函数传递附加参数在Internet Explorer中无效。*是否有任何理由在这里做,即内部函数不能访问其父'玩家'(或星期五毁了我的大脑)? – alex 2011-04-29 04:47:22

+0

你是对的@alex,我更新了我的回复。 – CarlosZ 2011-04-29 05:09:14

+0

+1最有可能做OP的要求,并高兴我的大脑仍然OK :) – alex 2011-04-29 05:12:37