0

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.

I want to implement one more method like stopTimer which stop that startTimer's interval, I know I have to user 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:

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..

Please guide me is this approach is correct?? or how do i make it correct..

2
  • you should get it's id in a variable while creating the timer, & pass it to clear function Commented Oct 4, 2012 at 10:08
  • @AshokRaj i had into the variable this.interval = setInterval(function(){..}, 1000) but i don't know how do i access this.interval into the stopTimer().. Commented Oct 4, 2012 at 10:09

3 Answers 3

1

Modified code. save return value of setInterval in setIntervalConst and use it clearInterval. You should use same instance of jpTWUI while calling startTimer and stopTimer.

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 ); ....}
}
Sign up to request clarification or add additional context in comments.

3 Comments

can i use something like this?? var callHandler = new jpTWUI('timerCon', device); callHandler.startTimer(); and var callHandler = new jpTWUI('timerCon', device); callHandler.stopTimer();
Yes you can. are you recreating jpTWUI object? you should intialize callHandler only once.
can i do something like this var callHandler = new jpTWUI('timerCon', device); and call the methods separately like in a condition i want to call callHandler.startTimer(); and in second condition i want to call callHandler.stopTimer(); is this possible??
0

get the id when creating and clear it.

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

does this help your prob?

Comments

0

setInterval returns an identifier that you can pass to clearInterval to stop a timer, so you could do something like this:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.