R
R
RemiFokz2018-06-03 18:20:39
JavaScript
RemiFokz, 2018-06-03 18:20:39

Why does the timer work in chrome but not in others?

The timer is written through a class, everything works fine in chrome, but in other browsers for some reason the date takes the value 0 immediately.
The code:

$(document).ready(function() {
    let timer = new TimerToDate('.timer', '2018-06-31 20:00:00');
    timer.start();
    console.log(timer);
});



class Timer{
    constructor(selector, time){
        this.time = time;
        this.el = $(selector);
        this._timer;
        this.render();
    }

    tick(){
        this.time--;
        this.render();

        if(this.time <= 0){
            this.stop();
        }
    }

    render(){
        this.el.innerHTML = this.time;
    }

    start(){
        this._timer = setInterval(() => {
            this.tick();
        }, 1000);
    }

    stop(){
        clearInterval(this._timer);
    }
}

class TimerFormat extends Timer{

    getTimeItems(t){
        let items = {};
        items.d = parseInt(t / 86400);
        items.h = parseInt((t % 86400)/3600);
        let h_s = this.time % 3600;
        items.m = parseInt(h_s / 60);
        items.s = h_s % 60;

        return items;
    }

    render(){
        let items = this.getTimeItems(this.time);

    //    this.el.innerHTML = `${items.h}:${items.m}:${items.s}`;
    }

}

class TimerWords extends TimerFormat{

    getEnding(num, var1, var2, var3){
        let i1 = num % 100;
        let i0 = num % 10;

        if(i1 >= 5 && i1 < 20){
            return var1;
        }
        else if(i0 >= 2 && i0 <= 4){
            return var2;
        }
        else if(i0 == 1){
            return var3;
        }
        else{
            return var1;
        }
    }

    render(){
        let items = this.getTimeItems(this.time);
        let dW = this.getEnding(items.d, 'дней', 'дня', 'день')
        let hW = this.getEnding(items.h, 'часов', 'часа', 'час');
        let mW = this.getEnding(items.m, 'минут', 'минуты', 'минута');
        let sW = this.getEnding(items.s, 'секунд', 'секунды', 'секунда');

//this.el.innerHTML = `${items.h} ${hW} ${items.m} ${mW} ${items.s} ${sW}`;
        $('.days').html(items.d + '<span>' + dW + '</span>');
        $('.hours').html(items.h + '<span>' + hW + '</span>');
        $('.min').html(items.m + '<span>' + mW + '</span>');
        $('.sec').html(items.s + '<span>' + sW + '</span>');
    }

}

class TimerToDate extends TimerWords{

    constructor(selector, dt){
        let target = new Date(dt);
        let now = new Date();
        let t = parseInt((target.getTime() - now.getTime()) / 1000);
        super(selector, t > 0 ? t : 0);
    }

    tick(){
        super.tick();
        if(this.time <= 0){
            $('.timer').children().remove();
            $('.timer').html("Опоздали!");
        }
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tyzberd, 2018-06-04
@tyzberd

see support
https://developer.mozilla.org/en/docs/Web/JavaScri...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question