html5音频播放很多次

时间:2016-08-19 20:47:31

标签: jquery html5 audio

var audio = new Audio('click.mp3');
$("nav li").mouseover(function() {
  audio.play();
}).mouseout(function() {
  audio.pause(); 
});

这是我的代码,在悬停音频文件时播放很多次,我想知道要添加什么来让它在悬停时只播放一次不是很多次

1 个答案:

答案 0 :(得分:1)

var audio = new Audio('click.mp3');
var hasBeenPlayed = false;
$("nav li").mouseover(function() {
  if(!hasBeenPlayed) {
  audio.play();
  hasBeenPlayed = true;
}
}).mouseout(function() {
  audio.pause();
  hasBeenPlayed = false; 
});