2016-01-09 30 views
0

任何人都可以解釋爲儘可能簡單(因缺乏時間學習js非常慢)如何迭代同一段代碼。我需要在短時間內重複播放聲音,而不使用事件觸發器,使用Math.Random。何重用(迭代)代碼,無事件觸發器

var birdsSing = Math.floor((Math.random() * 10) + 1); 
    if (birdsSing === 1){ 
     birds.play(); 
    } 
+1

看看'setInterval' HTTPS://developer.mozilla .org/en-US/docs/Web/API/WindowTimers/setInterval – nvartolomei

+0

謝謝! ))) – geliokant

回答

2

使用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); 
+0

謝謝! ))) – geliokant

相關問題