Sarah Davies
Sarah Davies

Reputation: 15

Having trouble with stopping interval [JS]

Ok so this is starting to make me go a little crazy.

I've got a code for a timer tracker. You enter a time and it will calculate how long until the item will go on bid and when the bid phase will end. You can set an alarm as well.

My problem is that I am working with local variables. It all works fine until you delete the row. If the row is deleted, the interval will continue and the alarm will still go off. I've been thinking of a way to go around this. The only thing I can think of is checking if the row exists inside the insRow function but I'm not sure how to do that. If that's possible I could add it to this if statement:

    if(noticeTime[1] === -6) {
        clearInterval(interval);
    }

http://jsfiddle.net/t7h9tbzf/

Upvotes: 0

Views: 47

Answers (1)

AuxTaco
AuxTaco

Reputation: 5171

setInterval returns a numeric ID that you can store as a data- attribute in the inserted row, with something like

const interval = window.setInterval(alarm, 1000);
new_row.dataset.intervalID = interval;

Then, before deleting the row, clear the interval:

window.clearInterval(row.dataset.intervalID);

Upvotes: 1

Related Questions