简体   繁体   English

在javascript类之外访问函数

[英]access functions outside the class in javascript

My class structure is something like this: 我的班级结构是这样的:

var jpTWUI = function(id, connection){
    this.id = id;
    this.muteButton = "#mute";
    this.hangupButton = "#hang";
    this.transferButton = "#trans";
    this.parentElement = jQuery('#timerCon');
    this.connection = connection;

    this.interval = "";

    this.createElements();
    this.addEvents(); 
};

jpTWUI.prototype = { 
    createElements: function(){ ... }, 
    addEvents: function(){...}, 
    startTimer: function(){...}
}

Now I have created an object and called the class something like this 现在,我创建了一个对象,并将该类称为

var callHandler = new jpTWUI('timerCon', connection);
callHandler.startTimer();

But the problem is that the method startTimer has the setInterval function which display the duration in minutes and seconds. 但是问题在于方法startTimer具有setInterval函数,该函数以分钟和秒为单位显示持续时间。

I want to implement one more method like stopTimer which stop that startTimer 's interval, I know I have to user window.clearInterval . 我想实现另一种方法,如stopTimer ,该方法停止该startTimer的间隔,我知道我必须使用window.clearInterval but when I implemented the function stopTimer in the same class then I don't know how do I access that method with the class like: 但是当我在同一个类中实现功能stopTimer ,我不知道如何使用类似的类来访问该方法:

var callHandler = new jpTWUI('timerCon', device);
callHandler.stopTimer();

Hope you guys understand what I want to achieve, this is first time I have used the class in javascript.. 希望大家理解我要实现的目标,这是我第一次使用javascript中的类。

Please guide me is this approach is correct?? 请指导我这种方法是正确的吗? or how do i make it correct.. 或我如何使它正确。

Modified code. 修改后的代码。 save return value of setInterval in setIntervalConst and use it clearInterval. 将setInterval的返回值保存在setIntervalConst中,并使用它clearInterval。 You should use same instance of jpTWUI while calling startTimer and stopTimer. 在调用startTimer和stopTimer时,应使用jpTWUI的相同实例。

var callHandler = new jpTWUI('timerCon', connection);
callHandler.startTimer();

///var callHandler = new jpTWUI('timerCon', device); // remove this line if you want access same instance of jpTWUI.
callHandler.stopTimer();

.

jpTWUI.prototype = { 
    createElements: function(){ ... }, 
    addEvents: function(){...}, 
    setIntervalConst: 0,
    startTimer: function(){ this.setIntervalConst = setInterval(....) ....},
    stoptTimer: function(){ clearInterval(this.setIntervalConst ); ....}
}

get the id when creating and clear it. 在创建并获取ID时将其清除。

this.interval = setInterval(function(){..}, 1000)
clearInterval(this.interval );

does this help your prob? 这对您的问题有帮助吗?

setInterval returns an identifier that you can pass to clearInterval to stop a timer, so you could do something like this: setInterval返回一个标识符,您可以将该标识符传递给clearInterval来停止计时器,因此您可以执行以下操作:

jpTWUI.prototype = { 
    …
    startTimer: function(){
        this.timer = setInterval(function(){
            console.log("interval")
        },2000) // every 2 seconds
    },
    stopTimer: function(){
        if(this.timer){
            clearInterval(this.timer);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM