JavaScript 函数每次运行时循环次数是原来的两倍

发布于 2024-10-06 23:52:33 字数 1281 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2024-10-13 23:52:33

您每次都会绑定一个额外点击处理程序,如果您想更改它,只需 首先 .unbind() ,如下所示:

 $('#yes').fadeIn(500).unbind("click").click(function(){

...与 #no 相同,否则每次调用 .click() 您正在附加一个新的 click 事件处理程序,但不是删除前一个,因此两者都会被执行......并且每次都会添加一个额外的。

You're binding an additional click handler each time, if you want to change it, just .unbind() first, like this:

 $('#yes').fadeIn(500).unbind("click").click(function(){

...and the same for #no, otherwise each time you call .click() you're attaching a new click event handler, but not removing the previous one, so both get executed...and an additional one added each time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文