<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="play">播放声音</button>
<script type="text/javascript">
var AudioContext = AudioContext || webkitAudioContext,
context = new AudioContext(),
canClick = true,
timeOut
function makeNoise() {
console.log('函数执行了!')
if (canClick) {
canClick = false
var oscillator = context.createOscillator()
oscillator.connect(context.destination)
oscillator.frequency.value = 500
oscillator.start(0)
setTimeout(function() {
oscillator.disconnect()
clearTimeout(timeOut)
canClick = true
}, 1500)
}
}
var player = document.getElementById('play')
player.onclick = function() {
makeNoise()
}
</script>
</body>
</html>