任何人都可以解釋爲儘可能簡單(因缺乏時間學習js非常慢)如何迭代同一段代碼。我需要在短時間內重複播放聲音,而不使用事件觸發器,使用Math.Random。何重用(迭代)代碼,無事件觸發器
var birdsSing = Math.floor((Math.random() * 10) + 1);
if (birdsSing === 1){
birds.play();
}
任何人都可以解釋爲儘可能簡單(因缺乏時間學習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);
謝謝! ))) – geliokant
看看'setInterval' HTTPS://developer.mozilla .org/en-US/docs/Web/API/WindowTimers/setInterval – nvartolomei
謝謝! ))) – geliokant