您可以使用 Sound.js 在播放声音文件后的 5 秒执行一个动作吗?

Can you perform an action at 5 seconds into playing a sound file using Sound.js?

我已经创建了我的声音并在所有设备上正常播放我的 html5 页面。 当音频达到特定点时,我想显示一些动画。 可能在 5、10、25 秒。

这是否可能,如果可以,您能否提供示例代码以在特定时间间隔调用函数?

您可以使用 setTimeout() 非常简单地实现此目的:

// Set up functions that will be triggered during sound playback...
var a = function(){
    console.log("Do something after 5 seconds");
}

var b = function(){
    console.log("Do something after 10 seconds");
}

var c = function(){
    console.log("Do something after 25 seconds");
}

// Play the sound...
createjs.Sound.play("page1");

// Immediately after playing the sound, trigger the time out functions...
setTimeout(a, 5000);  // Triggers after 5 seconds of playback
setTimeout(b, 10000); // Triggers after 10 seconds of playback
setTimeout(c, 25000); // Triggers after 25 seconds of playback

Working example

有关 setTimeout 的更多信息可在此处找到:http://javascript.info/tutorial/settimeout-setinterval

setTimeout

The syntax is: var timerId = setTimeout(func|code, delay)

func|code – Function variable or the string of code to execute.

delay – The delay in microseconds, 1000 microseconds = 1 second. The execution will occur after the given delay.