Javascript“ .attr”在ie11和优化问题上不起作用

时间:2019-01-23 02:38:37

标签: javascript jquery internet-explorer-11 countdowntimer

在以下JavaScript上正常工作。

但是,我不知道如何在首次加载时检查是否有“数据结束日期”值。

这是一个使用结束日期倒计时的JavaScript。

例如: (实际上,li被公开为for声明)

<ul>
<li>
<div id="time-wrap" class="time-countdown" data-end-date="2019-01-25 14:00:00"></div>
</li>
<li>
<div id="time-wrap" class="time-countdown" data-end-date=""></div>
</li>
<li>
<div id="time-wrap" class="time-countdown" data-end-date="2019-01-27 14:00:00"></div>
</li>
<li>
<div id="time-wrap" class="time-countdown" data-end-date=""></div>
</li>
<li>
<div id="time-wrap" class="time-countdown" data-end-date="2019-01-29 14:00:00"></div>
</li>
<li>
<div id="time-wrap" class="time-countdown" data-end-date="2019-01-29 14:00:00"></div>
</li>
.
.
.
</ul>
$('.time-countdown').each(function(i){
        var timeSaleWrap = $(this);
        var serialDate = timeSaleWrap.data('end-date');//data value
        var endDate = new Date(serialDate); 

        timeSaleWrap.attr('id', "time-wrap" + (i + 1));
        Timesalescounter(endDate, 'time-wrap' + (i + 1));

        function Timesalescounter(dt, id) {

            var endTime = new Date(dt);
            var slaeEnd = endTime.getTime(dt);
            var evEdate = slaeEnd;

            var timer;


            function actuionWrap() {

            var startTime = new Date();
            var today = startTime.getTime();
            var evSdate = today;
            var deltaTime = evEdate - evSdate;

            var s = Math.floor(deltaTime / 1000);
            var m = Math.floor(s / 60);
            var h = Math.floor(m / 60);
            var d = Math.floor(h / 24);

            h %= 24;
            m %= 60;
            s %= 60;

            var weeks = Math.floor(d / 7);
            var d = d - weeks * 2;

            var timeS = Math.floor( evSdate / 1000 );
            var timeE = Math.floor( evEdate / 1000 );
            var timeT = Math.floor( timeE - timeS );

                if ( !timeT || timeT < 0 ) {
                    document.getElementById(id).style.display = "none";
                    clearInterval(timer);
                    document.getElementById(id).textContent = "00h";
                    document.getElementById(id).textContent = "00m";
                    document.getElementById(id).textContent = "00s";

                    return;
                } else {

                    h = (h < 10) ? "0" + h : h;
                    m = (m < 10) ? "0" + m : m;
                    s = (s < 10) ? "0" + s : s;

                    var time_content= "<div class='clock-wrap'>"+
                        "<div class='days cell' title='Days'>"+d+"<span id='sp_days'>days</span></div>"+
                        "<div class='hours cell' title='Hours'>"+h+"<span id='sp_hours'>hours</span></div>"+
                        "<div class='mins cell' title='Minutes'>"+m+"<span id='sp_minutes'>mins</span></div>"+
                        "<div class='secs cell' title='Seconds'>"+s+"<span id='sp_seconds'>secs</span></div>"+
                        "</div>";

                    document.getElementById(id).innerHTML = time_content;


                }

            } //actuionWrap

            timer = setInterval(actuionWrap, 1000);

        }
    });

现在有5个示例,但实际上可能有100多个。

此JavaScript似乎需要很长时间才能首次加载页面。

“ data-end-date”是否有值,我不必检查吗?

或者如果我有一个“数据结束日期”值并且不进行任何检查,会不会更慢?

您如何优化它?

添加: 为什么在IE11中不起作用?没有错误/错误显示。

timeSaleWrap.attr('id', "timesale-wrap" + (i + 1));

它不起作用。

0 个答案:

没有答案