lundi 17 décembre 2018

Interval Timer Running Twice when I click again

i make timer duration on my web, when i clicked button again. its trigger the interval twice, what should i do?, i wont disable my button after trigger. how to make the button not trigger timer again? when i click the button. thanks

this is my code.

function timeChatCounter(timeZone, nowDate, dateDiff, componentId, status) {
        this.componentId = componentId;
        this.counterIndex = 0;
        
        this.startChatCounter = function (){
                if (status != "Inactive" && status != "") {
                        var distance;
                        var counter = setInterval(function() {
                                var browserTimeZone = moment.tz.guess();
                                if (distance == null){
                                        var currentTime;
                                        var statusTime;
                                        if(browserTimeZone !== timeZone){
                                                currentTime = moment(moment.tz(nowDate, browserTimeZone)).toDate().getTime();
                                                statusTime = moment(moment.tz(dateDiff, browserTimeZone)).toDate().getTime();
                                        } else{
                                                currentTime = moment(nowDate).toDate().getTime();
                                                statusTime = moment(dateDiff).toDate().getTime();
                                        }
                                        distance = currentTime - statusTime;
                                } else {
                                        distance = distance + 1000;
                                }
                                // Time calculations for days, hours, minutes and seconds
                                var days = Math.floor(distance / (1000 * 60 * 60 * 24));
                                var hours = Math.floor((distance % (1000 * 60 * 60 * 24))
                                                / (1000 * 60 * 60));
                                var minutes = Math.floor((distance % (1000 * 60 * 60))
                                                / (1000 * 60));
                                var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        
                                // Output the result in an element with id="demo"
                                if (days > 0) {
                                        document.getElementById(componentId).innerHTML = days + "d "
                                        + hours + "h " + minutes + "m " + seconds + "s ";
                                } else if (hours > 0) {
                                        document.getElementById(componentId).innerHTML = hours + "h "
                                        + minutes + "m " + seconds + "s ";
                                } else if (minutes > 0) {
                                        document.getElementById(componentId).innerHTML = minutes + "m "
                                        + seconds + "s ";
                                } else {
                                        document.getElementById(componentId).innerHTML !== null
                                }
                        }, 1000);
                        counters.push(counter);
                        this.counterIndex = counters.length - 1;
                } else {
                        var element = document.getElementById(componentId); 
                        element.parentNode.removeChild(element);
                }
        }
        
        function clearChatCounter() {
                clearInterval(days);
        }
}



Aucun commentaire:

Enregistrer un commentaire