需求:每隔3分钟播放声音,提醒计时。间隔时间可以自行设置
<html> <meta charset=utf-8> <title>阅读定时器</title> <body> 间隔秒数:<input type="text" id="clock" value=180 /> <button onclick="createInt()">创建</button> <script type="text/javascript"> var id=1; var int; var ts; var ts_id=1; function createInt(){ id=1; var t1=document.getElementById("clock").value*1000; int=self.setInterval(" playAudio('8d5ec196.mp3')",t1); document.getElementById("seqence").value=id; //每秒+1 ts_id=1; ts=self.setInterval(" tsAdd1()",1000); } function clock() { var d=new Date(); var t=d.toLocaleTimeString(); document.getElementById("clock").value=t; } function tsAdd1() { ts_id=ts_id+1; document.getElementById("ts1").value=ts_id; } function playAudio(file) { var music=document.getElementById("music"); var embed=document.getElementById("bgsoundid"); if(embed){ music.innerHTML=""; } embed = document.createElement("embed"); embed.setAttribute('id', "bgsoundid"); embed.setAttribute('src', file); embed.setAttribute('hidden', true); embed.setAttribute('autostart', true); embed.setAttribute('loop', "1"); music.appendChild(embed); id++ document.getElementById("seqence").value=id; } function clearTime(){ int=window.clearInterval(int); ts=window.clearInterval(ts); document.getElementById('seqence').value=-1; document.getElementById("ts1").value=-1; } </script> <button onclick="clearTime()">停止</button> <br/> 次 数:<input type="text" id="seqence" readonly value=-1 /> <br/> 开始状态:<input type="text" id="ts1" readonly value=-1 /> <div id="music"></div> </body> </html>