任何人都可以尽可能简单地解释一下(由于缺乏时间,学习 js 非常慢)如何迭代同一段代码。我需要在短暂的延迟后一次又一次地重播声音,使用 Math.Random 而不使用事件触发器。
var birdsSing = Math.floor((Math.random() * 10) + 1);
if ( birdsSing === 1){
birds.play();
}
最佳答案
使用setInterval()
函数:
// Milliseconds between sound playbacks
// Change this value to how often you want the sound to play
var playbackInterval = 500;
setInterval(function() {
var birdsSing = Math.floor((Math.random() * 10) + 1);
if (birdsSing === 1) { birds.play(); }
}, playbackInterval);
关于javascript - 如何在没有事件触发器的情况下重用(迭代器)代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696706/