momentJS Timer不适用于Firefox

时间:2016-02-11 11:49:54

标签: javascript html firefox setinterval momentjs

我通过Moment.js库实现了一个倒数计时器,不幸的是它在Firefox上不起作用 这是我的代码:

function createTimer(begin, timeUp) {
    var timer = document.getElementById('timer');
    var timerDays = document.getElementById('days').children[0];
    var timerHours = document.getElementById('hours').children[0];
    var timerMinutes = document.getElementById('minutes').children[0];
    var timerSeconds = document.getElementById('seconds').children[0];

    var intervalID = window.setInterval(function () {
        // Difference between timeUp and now
        var differenceToTimeUp = moment.duration(timeUp.diff(moment()));
        // Difference between begin and now
        var differenceToBegin = moment.duration(begin.diff(moment()));
        if (differenceToTimeUp.asSeconds() > 0) {
            timer.classList.remove('hidden');
        } else {
            timer.classList.add('hidden');
        }
        timerDays.innerText = ('0' + differenceToTimeUp.days()).slice(-2);
        timerHours.innerText = ('0' + differenceToTimeUp.hours()).slice(-2);
        timerMinutes.innerText = ('0' + differenceToTimeUp.minutes()).slice(-2);
        timerSeconds.innerText = ('0' + differenceToTimeUp.seconds()).slice(-2);
    }, 1000);

}
document.addEventListener('DOMContentLoaded', function () {

//        // Comment out for production
//        var test1 = moment('2016-02-02 11:00:00');
//        var test2 = moment('2016-03-11 11:00:00');
//        createTimer(test1, test2);

    var now =  moment(new Date(moment())).utc().format("YYYY-MM-DD HH:mm:ss");

    var firstStart = moment('2016-02-11 11:00:00');
    var firstEnd = moment('2016-02-15 17:00:00');

    var secondStart = moment('2016-02-16 14:00:00');
    var secondEnd = moment('2016-02-17 17:00:00');

    if (now > firstStart._i && now < firstEnd._i) {
        createTimer(firstStart, firstEnd);
    }

});

在调试器中我可以看到那个时刻正在获取日期,所以我认为它与setInterval函数有关。
有什么想法吗?

更新

搞定了。这个错误实际上不是关于momentJS的。使用.innerText更改Text元素不适用于InternetExplorer。使用.textContent修复它。当我使用.tff时,我也在InternetExplorer上使用我的自定义字体。使用.woff工作正常。

0 个答案:

没有答案