javascript - 如何在没有事件触发器的情况下重用(迭代器)代码

标签 javascript

任何人都可以尽可能简单地解释一下(由于缺乏时间,学习 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/

相关文章:

javascript - 页面中有 2 个表的表排序器问题

javascript - 由于 "TypeError: ... is not a constructor",无法实例化 UI5 类

javascript - 环回 - 在 "after save" Hook 中覆盖之前查看数据

javascript - 如何根据其内部内容设置 iframe 的大小?

javascript - 如何让我的获取错误显示 http 状态代码?

javascript - Angular js : accessing $scope from jQuery

javascript - AWS Lambda Node Js JSON 未定义

javascript - 如何在 Angular 2 的组件中使用管道?

javascript - X 轴上的 Nivo Line Formatting 时间刻度

javascript - 在每个对象数组之间插入一个字符串,最后一个标记具有特定字符串