v2.5.2
Giriş yap

setInterval, ilk önce temizleyip ardından yeniden çalıştırmak

siyahkalem
293 defa görüntülendi

Merhaba,
Başlıkta olduğu gibi setInterval'i önce temizleyip ardından gönderdiğim veriyi çalıştırmasını istiyorum
ama sorun şu ki clearInterval kullandığımda tezmileyp durduruyor sadece

    // item.tiktak -> her seferinde tıklandığında farklı bir tarih göndermektedir.
    
    
    const launchDate = new Date(item.tiktak);

                let x = setInterval(() => {
                    const currentDate = new Date();
                    const launchTime = launchDate - currentDate;

                    const days = Math.floor(launchTime / (1000 * 60 * 60 * 24));
                    const hours = Math.floor((launchTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                    const minutes = Math.floor((launchTime % (1000 * 60 * 60)) / (1000 * 60));
                    const seconds = Math.floor((launchTime % (1000 * 60)) / 1000);

                    this.seconds = seconds;
                    this.minutes = minutes;
                    this.hours = hours;
                    this.days = days;

                    if (minutes < 0 && seconds < 0) {

                        this.seconds = 0;
                        this.minutes = 0;
                        this.hours = 0;
                        this.days = 0;

                    }

                    if (days < 0 && hours < 0 && minutes < 0 && seconds < 0) {
                        clearInterval(x);
                    }


                }, 1000)
                
                clearInterval(x)
                setTimeout(() =>{x}, 1000)
                
siyahkalem
596 gün önce
// data
polling : null

// methods
           if (item.periodValidityTime > 0) {
                
                // Seçilen data öncesi çalışan veriler için temizleniyor
                // setTimeout ile yeniden başlatıyoruz
                clearInterval(this.polling);
                setTimeout(() => {this.polling}, 1000);

                const launchDate = new Date(item.tiktak);

                this.polling = setInterval(() => {
                    const currentDate = new Date();
                    const launchTime = launchDate - currentDate;

                    const days = Math.floor(launchTime / (1000 * 60 * 60 * 24));
                    const hours = Math.floor((launchTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                    const minutes = Math.floor((launchTime % (1000 * 60 * 60)) / (1000 * 60));
                    const seconds = Math.floor((launchTime % (1000 * 60)) / 1000);

                    this.seconds = seconds;
                    this.minutes = minutes;
                    this.hours = hours;
                    this.days = days;

                    if (minutes < 0 && seconds < 0) {

                        this.seconds = 0;
                        this.minutes = 0;
                        this.hours = 0;
                        this.days = 0;

                    }

                    if (days < 0 && hours < 0 && minutes < 0 && seconds < 0) {
                        clearInterval(this.polling);
                    }


                }, 1000)


            } else {
                clearInterval(this.polling);
            }