- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
JavaScript倒数计时器:
当计时器达到零时,我想播放录音。我已经开始播放音频,但是在开始播放后几秒钟,相同的音频会再次以原始音频播放,并且永不停止。
这是我的代码:
//Javascript Countdown Timer.
//set the amount of time click
//start timer and the clock will
//count down until it reaches zero
var message = "Complete.";
var parselimit = 0;
var st = 0;
var limit, curhour, curmin, cursec;
function set_limit(strtstop) {
//prevent multiple settimeouts
if (st) {
clearTimeout(st);
}
if (document.getElementById("pause").value == 1) {
limit = document.getElementById("show_timer").innerHTML;
} else {
limit = document.getElementById("hours").value + ":" + document.getElementById("min").value + ":" + document.getElementById("sec").value;
}
parselimit = limit.split(":");
// take the minutes x 60 add it to the seconds;
parselimit = parselimit[0] * 3600 + parselimit[1] * 60 + parselimit[2] * 1;
//exit if timer wasn't set. //
if (parselimit <= 0) {
return;
}
if (strtstop == 1) {
clearTimeout(st);
} else {
st = setTimeout("begintimer()", 1000);
}
}
function begintimer() {
if (parselimit == 1) {
//ding ding ding
///////////////////////////////////ISSUE STATEMENT//////
var audio = new Audio('alarmbuzzer.mp3');
audio.play();
alert("starting audio");
/////////////////////////Audio Playing///////////////////
} else {
parselimit -= 1;
curhour = Math.floor(parselimit / 3600);
//alert(parselimit)
/* greater than an hour divide by
60 but subtract the hours. */
if (parselimit > 3600) {
//first convert hours back into seconds
curmin = curhour * 3600;
//subtract that from total to get minutes left.
curmin = parselimit - curmin;
curmin = Math.floor(curmin / 60);
//alert(curmin);
} else {
curmin = Math.floor(parselimit / 60);
}
cursec = parselimit % 60;
}
curmin += "";
if (curmin.length == 1 || curmin == 9) {
curmin = "0" + curmin;
}
cursec += "";
//alert(cursec);
if (cursec.length == 1 || cursec == 9) {
cursec = "0" + cursec;
}
curtime = curhour + ":" + curmin + ":" + cursec;
//alert(document.getElementById("show_timer").innerHTML);
document.getElementById("show_timer").innerHTML = curtime;
st = setTimeout("begintimer()", 1000);
}
function add_time_clock(x, frmelm) {
var ter = document.getElementById(frmelm).value;
/* add time */
if (x == 1) {
ter = eval(ter) + 1;
//if greater than 60 go to zero. i'm maxing out the hours at 60 too.
if (ter >= 60) {
ter = '00';
}
} else {
/* subtract time */
ter = eval(ter) - 1;
//if greater than 60 go to zero. i'm maxing out the hours at 60 too.
if (ter <= 0) {
ter = '00';
}
}
ter = "" + ter;
if (ter.length == 1) {
ter = "0" + ter;
}
document.getElementById(frmelm).value = ter;
}
function rset_tmr() {
document.getElementById('show_timer').innerHTML = "00:00:00";
document.getElementById('pause').value = 0;
}
function show_hide(div) {
if (document.getElementById(div).style.display == 'none') {
document.getElementById(div).style.display = '';
} else {
document.getElementById(div).style.display = 'none';
}
}
<div id="set_timer" style="">
<input id="pause" type="hidden" value="0" />
<br />
<table border="3" bordercolor="#0047B2" cellspacing="12">
<tbody>
<tr>
<td align="center" colspan="8" height="40">
<b><span id="show_timer">00:30:00</span></b>
</td>
</tr>
<tr>
<td>
Hrs</td>
<td>
Min</td>
<td>
Sec</td>
</tr>
<tr>
<td>
<input id="hours" maxlength="10" size="4" type="text" value="00" />:</td>
<td>
<input id="min" maxlength="10" size="4" type="text" value="30" />:</td>
<td>
<input id="sec" maxlength="10" size="2" type="text" value="00" />
</td>
</tr>
<tr>
<td>
[<a href="javascript:add_time_clock('1','hours')" data-cke-saved-href="javascript:add_time_clock('1','hours')">+</a>] [<a href="javascript:add_time_clock('2','hours')">-</a>]</td>
<td>
[<a href="javascript:add_time_clock('1','min')">+</a>] [<a href="javascript:add_time_clock('2','min')">-</a>]</td>
<td>
<a href="javascript:add_time_clock('1','sec')">+</a> <a href="javascript:add_time_clock('2','sec')">-</a>]</td>
</tr>
<tr>
<td align="center" colspan="3">
<input onclick="set_limit()" type="button" value="Start" />
<input onclick="set_limit('1'),document.getElementById('pause').value=1" type="button" value="Pause" />
<input onclick="rset_tmr()" type="button" value="Reset" />
</td>
</tr>
</tbody>
</table>
<br />
</div>
最佳答案
原因是您重复setTimeout
的代码不在else
的begintimer
子句之外。因此,即使倒数计时已经结束,您也可以执行另一个setTimeout
,当它触发时,您仍然在结尾处,因此它将再次启动音频,然后继续。移动代码以将计时器重复到else
子句中:
function begintimer(){
if (parselimit==1) {
//ding ding ding
///////////////////////////////////ISSUE STATEMENT//////
var audio = new Audio('alarmbuzzer.mp3');
audio.play();
/////////////////////////Audio Playing///////////////////
} else{
parselimit-=1;
curhour=Math.floor(parselimit/3600);
//alert(parselimit)
/* greater than an hour divide by
60 but subtract the hours. */
if(parselimit > 3600) {
//first convert hours back into seconds
curmin = curhour * 3600;
//subtract that from total to get minutes left.
curmin = parselimit - curmin;
curmin = Math.floor(curmin/60);
//alert(curmin);
} else {
curmin = Math.floor(parselimit/60);
}
cursec=parselimit%60;
curmin += "";
if(curmin.length == 1 || curmin==9) {
curmin = "0" + curmin;
}
cursec += "";
//alert(cursec);
if(cursec.length == 1 || cursec==9) {
cursec = "0" + cursec;
}
curtime = curhour + ":" + curmin + ":" + cursec;
//alert(document.getElementById("show_timer").innerHTML);
document.getElementById("show_timer").innerHTML = curtime;
st=setTimeout("begintimer()",1000);
}
}
//Javascript Countdown Timer.
//set the amount of time click
//start timer and the clock will
//count down until it reaches zero
var message = "Complete.";
var parselimit = 0;
var st = 0;
var limit, curhour, curmin, cursec;
function set_limit(strtstop) {
//prevent multiple settimeouts
if (st) {
clearTimeout(st);
}
if (document.getElementById("pause").value == 1) {
limit = document.getElementById("show_timer").innerHTML;
} else {
limit = document.getElementById("hours").value + ":" + document.getElementById("min").value + ":" + document.getElementById("sec").value;
}
parselimit = limit.split(":");
// take the minutes x 60 add it to the seconds;
parselimit = parselimit[0] * 3600 + parselimit[1] * 60 + parselimit[2] * 1;
//exit if timer wasn't set. //
if (parselimit <= 0) {
return;
}
if (strtstop == 1) {
clearTimeout(st);
} else {
st = setTimeout("begintimer()", 1000);
}
}
function begintimer() {
if (parselimit == 1) {
//ding ding ding
///////////////////////////////////ISSUE STATEMENT//////
var audio = new Audio('alarmbuzzer.mp3');
audio.play();
alert("Starting audio");
/////////////////////////Audio Playing///////////////////
} else {
parselimit -= 1;
curhour = Math.floor(parselimit / 3600);
//alert(parselimit)
/* greater than an hour divide by
60 but subtract the hours. */
if (parselimit > 3600) {
//first convert hours back into seconds
curmin = curhour * 3600;
//subtract that from total to get minutes left.
curmin = parselimit - curmin;
curmin = Math.floor(curmin / 60);
//alert(curmin);
} else {
curmin = Math.floor(parselimit / 60);
}
cursec = parselimit % 60;
curmin += "";
if (curmin.length == 1 || curmin == 9) {
curmin = "0" + curmin;
}
cursec += "";
//alert(cursec);
if (cursec.length == 1 || cursec == 9) {
cursec = "0" + cursec;
}
curtime = curhour + ":" + curmin + ":" + cursec;
//alert(document.getElementById("show_timer").innerHTML);
document.getElementById("show_timer").innerHTML = curtime;
st = setTimeout("begintimer()", 1000);
}
}
function add_time_clock(x, frmelm) {
var ter = document.getElementById(frmelm).value;
/* add time */
if (x == 1) {
ter = eval(ter) + 1;
//if greater than 60 go to zero. i'm maxing out the hours at 60 too.
if (ter >= 60) {
ter = '00';
}
} else {
/* subtract time */
ter = eval(ter) - 1;
//if greater than 60 go to zero. i'm maxing out the hours at 60 too.
if (ter <= 0) {
ter = '00';
}
}
ter = "" + ter;
if (ter.length == 1) {
ter = "0" + ter;
}
document.getElementById(frmelm).value = ter;
}
function rset_tmr() {
document.getElementById('show_timer').innerHTML = "00:00:00";
document.getElementById('pause').value = 0;
}
function show_hide(div) {
if (document.getElementById(div).style.display == 'none') {
document.getElementById(div).style.display = '';
} else {
document.getElementById(div).style.display = 'none';
}
}
<div id="set_timer" style="">
<input id="pause" type="hidden" value="0" />
<br />
<table border="3" bordercolor="#0047B2" cellspacing="12">
<tbody>
<tr>
<td align="center" colspan="8" height="40">
<b><span id="show_timer">00:30:00</span></b>
</td>
</tr>
<tr>
<td>
Hrs</td>
<td>
Min</td>
<td>
Sec</td>
</tr>
<tr>
<td>
<input id="hours" maxlength="10" size="4" type="text" value="00" />:</td>
<td>
<input id="min" maxlength="10" size="4" type="text" value="30" />:</td>
<td>
<input id="sec" maxlength="10" size="2" type="text" value="00" />
</td>
</tr>
<tr>
<td>
[<a href="javascript:add_time_clock('1','hours')" data-cke-saved-href="javascript:add_time_clock('1','hours')">+</a>] [<a href="javascript:add_time_clock('2','hours')">-</a>]</td>
<td>
[<a href="javascript:add_time_clock('1','min')">+</a>] [<a href="javascript:add_time_clock('2','min')">-</a>]</td>
<td>
<a href="javascript:add_time_clock('1','sec')">+</a> <a href="javascript:add_time_clock('2','sec')">-</a>]</td>
</tr>
<tr>
<td align="center" colspan="3">
<input onclick="set_limit()" type="button" value="Start" />
<input onclick="set_limit('1'),document.getElementById('pause').value=1" type="button" value="Pause" />
<input onclick="rset_tmr()" type="button" value="Reset" />
</td>
</tr>
</tbody>
</table>
<br />
</div>
关于javascript - 为什么Javascript警报音频会不断重放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28490532/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!