JavaScript 函数每次运行时循环次数是原来的两倍
我正在尝试使用 JavaScript (+JQuery) 中的一个函数,它就像一个声音校准器系统。它从一个频段开始,播放它,询问用户是否可以听到它 - 然后如果可以,它知道系统的最低频率,如果不能,它会上升到下一个频段,然后重新检查等等,直到找到最低频率。
但是,当我运行该函数时,它似乎每次运行的次数都是原来的两倍。从 testFreqNum 警报中,我可以看到第一次添加 1 次,第二次添加 1 然后再次添加,第三次添加 4 次,然后 8 次等。
可能是一个容易修复的错误 - 我是新手JavaScript 所以也许你可以立即看到它?谢谢。
function testFreq(band, testType){
if (testType == 'testLow') {
$('#calibrationText').html('<h5>Testing ' + freqs[band] + 'Hz...</h>');
playSound('Tones', band);
//window.setTimeout(function(){
$('#calibrationText').html('<h5>Could you hear ' + freqs[band] + 'Hz clearly?</h>');
$('#yes').fadeIn(500).click(function(){
bandMin = band;//sets randGen's min availible band value
testFreqNum = 31;//sets test frequency to upper high
testFreq(testFreqNum, 'testHigh');//runs calibrator again in high mode starting band 31
$('#yes').fadeOut(500);
$('#no').fadeOut(500);
});
$('#no').fadeIn(500).click(function(){
testFreqNum = testFreqNum + 1;
alert(testFreqNum);
testFreq(testFreqNum, 'testLow');//runs calibrator again in low mode
//$('#yes').fadeOut(500);
//$('#no').fadeOut(500);
});
// }, 4000);
}
...其余的...
I'm trying to use a function in JavaScript (+JQuery) which is like a sound calibrator system. It starts at one frequency band, plays it, asks the user if they can hear it - then if the can it knows the lowest frequency of their system and if not it goes up to the next band and then re-checks etc. until the lowest frequency is found.
However, when I run the function, it seems to run itself twice as many times each time. From the testFreqNum alert I could see the first time it added 1 once, the second time it added 1 then added it again, third time it did it 4 times, then 8 etc.
Probably an easy error to fix - I'm new to JavaScript so maybe you can see it straight away? Thanks.
function testFreq(band, testType){
if (testType == 'testLow') {
$('#calibrationText').html('<h5>Testing ' + freqs[band] + 'Hz...</h>');
playSound('Tones', band);
//window.setTimeout(function(){
$('#calibrationText').html('<h5>Could you hear ' + freqs[band] + 'Hz clearly?</h>');
$('#yes').fadeIn(500).click(function(){
bandMin = band;//sets randGen's min availible band value
testFreqNum = 31;//sets test frequency to upper high
testFreq(testFreqNum, 'testHigh');//runs calibrator again in high mode starting band 31
$('#yes').fadeOut(500);
$('#no').fadeOut(500);
});
$('#no').fadeIn(500).click(function(){
testFreqNum = testFreqNum + 1;
alert(testFreqNum);
testFreq(testFreqNum, 'testLow');//runs calibrator again in low mode
//$('#yes').fadeOut(500);
//$('#no').fadeOut(500);
});
// }, 4000);
}
...and the rest...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您每次都会绑定一个额外点击处理程序,如果您想更改它,只需 首先
.unbind()
,如下所示:...与
#no
相同,否则每次调用.click()
您正在附加一个新的click
事件处理程序,但不是删除前一个,因此两者都会被执行......并且每次都会添加一个额外的。You're binding an additional click handler each time, if you want to change it, just
.unbind()
first, like this:...and the same for
#no
, otherwise each time you call.click()
you're attaching a newclick
event handler, but not removing the previous one, so both get executed...and an additional one added each time.