---
title: "html5 audio声音多次播放"
author: "onlyor"
date: "2014-12-17T09:37:58Z"
tags: []
categories: ["编程语言"]
slug: "html5-audio"
draft: false
lastmod: "2014-12-17T09:37:58Z"
id: 2166923
url: "https://onlyor.iteye.com/blog/2166923"
---

```
<html>
<head>
	<title>js</title>
</head>
<body>

    <input id="bt" type="button" value="BT" >
    
	<script type="text/javascript">
	   var count = 2;
	   var music;
	
		var bt = document.getElementById("bt");     
		bt.addEventListener("click",function() {
			count = 2;
			a();
		});
		
		// html5：audio声音多次播放
		function a() {
			if (!music) {
				music = new Audio();
				music.addEventListener("ended", function (){
					if (count > 0) {
						a();
					}
				});
			} else {
				music.pause();
			}
			
			music.src = 'ding.mp3';
			music.play();
			
			console.log(count);
			count--;
		}
		
	</script>
	
</body>


</html>
```