Javascript 如果功能处于活动状态,如何禁用单击

Javascript 如果功能处于活动状态,如何禁用单击,javascript,jquery,timer,disabled-control,Javascript,Jquery,Timer,Disabled Control,我在这里创建了我的函数()。现在我想禁用按钮单击,即play/pause如果声音像Get ready,5,4,3,2,1那样播放 我只知道如何禁用表单提交按钮,但我很困惑如何禁用在我的例子中的点击超链接 使用代码示例进行解释: 我想禁用这个 <a href="#" id="btn_start">PLAY</a> 警告:此JS fiddle演示可能在jquery中加载时播放声音,可以通过取消默认操作轻松完成。这是样品 $("#btn_start").click(funct

我在这里创建了我的函数()。现在我想禁用按钮单击,即
play/pause
如果声音像
Get ready,5,4,3,2,1那样播放

我只知道如何禁用表单提交按钮,但我很困惑如何禁用在我的例子中的点击超链接

使用代码示例进行解释:

我想禁用这个

<a href="#" id="btn_start">PLAY</a>

警告:此JS fiddle演示可能在jquery中加载时播放声音

,可以通过取消默认操作轻松完成。这是样品

$("#btn_start").click(function(event){      
    if(not_active_flag){
        // Prevent anchor to active
        return false;
    }else{
        // Anchor active as usual
        return true;
    }       
});

在您的情况下,该链接最终将调用
this.buttonHandler
,其代码如下:

App.prototype.buttonHandler = function (e) {
    e.preventDefault(); // prevent anchor default action
    this.toggle(); // toggle play/pause
};
由于在执行
playGetReady
之前附加了
buttonHandler
,因此不可能让
playGetReady
将单击处理程序附加到用于阻止另一个单击处理程序执行的锚元素

在这种情况下,注释中的@gp.解决方案很可能是最好的解决方案。在您的情况下,您甚至可以在应用程序中使用局部变量。如果使用全局变量,请使用
窗口引用它。yourglobalvariable
。如果使用局部变量,请确保在某个地方定义它,并用
this.yourviable
引用它。将按钮句柄更改为:

App.prototype.buttonHandler = function (e) {
    e.preventDefault(); // prevent anchor default action
    if( this.soundready )
        this.toggle(); // toggle play/pause
};
在适当的地方,使此变量
false
,以防止“按钮”工作。当按钮工作时,将变量更改为
true
。我认为这应该在您的问题代码中的
done()
之前,但您可能对代码的执行顺序有更好的了解。

您可以尝试此操作(在下面的函数中进行更改),但不确定这是否是您想要的,可能还有其他方法

App.prototype.start = function () {
    var self = this;
    // unbind for a while
    self.$button.unbind('click', self.buttonHandler); // <--
    var start = function () {
            // start countdown
            self.intervalHandle = setInterval($.proxy(self.tick, self), 1000);
            // bind again
            self.$button.click($.proxy(self.buttonHandler, self)); // <--
            // change button text to PAUSE
            self.$button.text('PAUSE');
        };

    if (this.newTimer) {
        playGetReady(start);
    } else {
        start();
    }
};
App.prototype.start=函数(){
var self=这个;
//解开一段时间

self.$button.unbind('click',self.buttonHandler);//为什么我没有收到上述问题的任何答案。我的问题是不正确的还是我这边的其他错误?欢迎来到StackOverflow。StackOverflow是一个大多数人类居住的地方。虽然有些巫师可以在6分钟内写出最惊人的答案,但大多数人类需要更多的答案。请…耐心等待…一个简单的解决方案…设置一个标志,例如
窗口。\uuu compilerbusy=true
运行计时器时,在计时器完成时将其设置为false。在btn单击处理程序中,检查标志。@RecoveringSince2003感谢您提醒我实际上我没有正确查看其他函数,但逻辑是相同的,可能不是相同的上下文。您能e关于您正在使用的功能的更多详细信息?我想您需要。检查
应用程序原型。结束
else
中的最后一行。很高兴知道,那么您将向我的帐户转入多少,lol!:pDid您尝试为您的网站获得Google Adsense
heera。它
App.prototype.start = function () {
    var self = this;
    // unbind for a while
    self.$button.unbind('click', self.buttonHandler); // <--
    var start = function () {
            // start countdown
            self.intervalHandle = setInterval($.proxy(self.tick, self), 1000);
            // bind again
            self.$button.click($.proxy(self.buttonHandler, self)); // <--
            // change button text to PAUSE
            self.$button.text('PAUSE');
        };

    if (this.newTimer) {
        playGetReady(start);
    } else {
        start();
    }
};