火狐在事件呼叫中使用相同的兰登号码

Firefox same randon number on event call

本文关键字:号码 事件 呼叫 火狐      更新时间:2023-09-26

我有一个与事件onended相关联的函数randomPlay但随机数在 Firefox 上总是相同的。在Chrome上没关系。任何人都可以解释为什么以及如何修复?

 function randomPlay(){
    $("audio").html('<source src="'+srcArray[Math.floor(Math.random() * srcArray.length)]+'" type="audio/mpeg">');
    document.getElementById('audio').play();
    }

元素是这个音频:

<audio id="audio" autoplay onended="randomPlay();" ><source src="music.mp3" type="audio/mpeg"></audio>

更新

我将元素更改为:

<audio id="audio" autoplay onended="randonPlay();" src=""></audio>

并将函数更改为:

function randonPlay(){
    $("audio").attr("src",srcArray[Math.floor(Math.random() * srcArray.length)]);
    document.getElementById('audio').play();
}

所以现在一切都很好:)谢谢大家的帮助。无论如何,我还找到了一个库来生成随机数的机会.js

这是因为在页面加载时生成器已播种。 有关一些信息,请参阅此帖子:

这里: Math.random() - 非随机