首页 > 解决方案 > 计时器与正确时间不同

问题描述

我在列表的实际时间和计时器代码显示的时间之间存在时间差。因此,对于列表,此代码将剩余时间显示为 8d 18h 23m 58s GTB 标准时间,而不是真正的 9d、1h、23m、45s。

任何人都可以查明为什么会发生这种情况,以及是否有任何解决方案来解决它(或者至少停止它以显示文本 GTB 标准时间,所以只是实际时间)。谢谢

function timelefttolistnewtimer(){

var arr = [];
var offset = getTimezoneName();

jQuery('.timelefttolistnewtimer').each(function(k,v) {
    
    var id = jQuery(this).attr('id');
    
    
    arr["countDownDate"+id] = new Date(jQuery(this).data('time')).getTime();
    

    // Update the count down every 1 second
    arr["x"+id] = setInterval(function() {

      // Get today's date and time
      arr["now"+id]= new Date().getTime();
        
      // Find the distance between now and the count down date
      arr["distance"+id] = arr["countDownDate"+id]- arr["now"+id];
        
      // Time calculations for days, hours, minutes and seconds
      arr["days"+id] = Math.floor(arr["distance"+id] / (1000 * 60 * 60 * 24));
      arr["hours"+id] = Math.floor((arr["distance"+id] % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      arr["minutes"+id] = Math.floor((arr["distance"+id] % (1000 * 60 * 60)) / (1000 * 60));
      arr["seconds"+id] = Math.floor((arr["distance"+id] % (1000 * 60)) / 1000);
        
      // Output the result in an element with id="demo"
      jQuery("#"+id).html(arr["days"+id] + "d " + arr["hours"+id] + "h "
      + arr["minutes"+id] + "m " + arr["seconds"+id] + "s " + offset);
    
      console.log('arr');
        console.log(arr["seconds"+id]);
        console.log(offset);
        
      // If the count down is over, write some text 
      if (arr["distance"+id] < 0) {
        clearInterval(arr["x"+id]);
        jQuery("#"+id).html("EXPIRED");
      }
    }, 1000);


});

}

function timelefttolistnewtimer1(){

var arr = [];
var offset = getTimezoneName();

jQuery('.timelefttolistnewtimer1').each(function(k,v) {
    
    var id = jQuery(this).attr('id');

    
    
    arr["countDownDate"+id] = new Date(jQuery(this).data('time')).getTime();
    

    // Update the count down every 1 second
    arr["x"+id] = setInterval(function() {

      // Get today's date and time
      arr["now"+id]= new Date().getTime();
        
      // Find the distance between now and the count down date
      arr["distance"+id] = arr["countDownDate"+id]- arr["now"+id];
        
      // Time calculations for days, hours, minutes and seconds
      arr["days"+id] = Math.floor(arr["distance"+id] / (1000 * 60 * 60 * 24));
      arr["hours"+id] = Math.floor((arr["distance"+id] % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      arr["minutes"+id] = Math.floor((arr["distance"+id] % (1000 * 60 * 60)) / (1000 * 60));
      arr["seconds"+id] = Math.floor((arr["distance"+id] % (1000 * 60)) / 1000);
        
      // Output the result in an element with id="demo"
     

      jQuery("#"+id).html(arr["days"+id] + "d " + arr["hours"+id] + "h "
      + arr["minutes"+id] + "m " + arr["seconds"+id] + "s " + offset);


      //console.log('arr');
        //console.log(arr["seconds"+id]);
        //console.log(offset);
        
      // If the count down is over, write some text 
      if (arr["distance"+id] < 0) {
        clearInterval(arr["x"+id]);
        jQuery("#"+id).html("EXPIRED");
      }
    }, 1000);


});

}

function getTimezoneName() {
var timeSummer = new Date(Date.UTC(2005, 6, 30, 0, 0, 0, 0));
var summerOffset = -1 * timeSummer.getTimezoneOffset();
var timeWinter = new Date(Date.UTC(2005, 12, 30, 0, 0, 0, 0));
var winterOffset = -1 * timeWinter.getTimezoneOffset();
var timeZoneHiddenField;

if (-720 == summerOffset && -720 == winterOffset) { timeZoneHiddenField = 'Dateline Standard Time'; }
else if (-660 == summerOffset && -660 == winterOffset) { timeZoneHiddenField = 'UTC-11'; }
else if (-660 == summerOffset && -660 == winterOffset) { timeZoneHiddenField = 'Samoa Standard Time'; }
else if (-660 == summerOffset && -600 == winterOffset) { timeZoneHiddenField = 'Hawaiian Standard Time'; }
else if (-570 == summerOffset && -570 == winterOffset) { timeZoneHiddenField.value = 'Pacific/Marquesas'; }
//                else if (-540 == summerOffset && -600 == winterOffset) { timeZoneHiddenField.value = 'America/Adak'; }
//                else if (-540 == summerOffset && -540 == winterOffset) { timeZoneHiddenField.value = 'Pacific/Gambier'; }
else if (-480 == summerOffset && -540 == winterOffset) { timeZoneHiddenField = 'Alaskan Standard Time'; }
//                else if (-480 == summerOffset && -480 == winterOffset) { timeZoneHiddenField = 'Pacific/Pitcairn'; }
else if (-420 == summerOffset && -480 == winterOffset) { timeZoneHiddenField = 'Pacific Standard Time'; }
else if (-420 == summerOffset && -420 == winterOffset) { timeZoneHiddenField = 'US Mountain Standard Time'; }
else if (-360 == summerOffset && -420 == winterOffset) { timeZoneHiddenField = 'Mountain Standard Time'; }
else if (-360 == summerOffset && -360 == winterOffset) { timeZoneHiddenField = 'Central America Standard Time'; }
//                else if (-360 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'Pacific/Easter'; }
else if (-300 == summerOffset && -360 == winterOffset) { timeZoneHiddenField = 'Central Standard Time'; }
else if (-300 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'SA Pacific Standard Time'; }
else if (-240 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'Eastern Standard Time'; }
else if (-270 == summerOffset && -270 == winterOffset) { timeZoneHiddenField = 'Venezuela Standard Time'; }
else if (-240 == summerOffset && -240 == winterOffset) { timeZoneHiddenField = 'SA Western Standard Time'; }
else if (-240 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'Central Brazilian Standard Time'; }
else if (-180 == summerOffset && -240 == winterOffset) { timeZoneHiddenField = 'Atlantic Standard Time'; }
else if (-180 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'Montevideo Standard Time'; }
else if (-180 == summerOffset && -120 == winterOffset) { timeZoneHiddenField = 'E. South America Standard Time'; }
else if (-150 == summerOffset && -210 == winterOffset) { timeZoneHiddenField = 'Mid-Atlantic Standard Time'; }
else if (-120 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'America/Godthab'; }
else if (-120 == summerOffset && -120 == winterOffset) { timeZoneHiddenField = 'SA Eastern Standard Time'; }
else if (-60 == summerOffset && -60 == winterOffset) { timeZoneHiddenField = 'Cape Verde Standard Time'; }
else if (0 == summerOffset && -60 == winterOffset) { timeZoneHiddenField = 'Azores Daylight Time'; }
else if (0 == summerOffset && 0 == winterOffset) { timeZoneHiddenField = 'Morocco Standard Time'; }
else if (60 == summerOffset && 0 == winterOffset) { timeZoneHiddenField = 'GMT Standard Time'; }
else if (60 == summerOffset && 60 == winterOffset) { timeZoneHiddenField = 'Africa/Algiers'; }
else if (60 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'Namibia Standard Time'; }
else if (120 == summerOffset && 60 == winterOffset) { timeZoneHiddenField = 'Central European Standard Time'; }
else if (120 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'South Africa Standard Time'; }
else if (180 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'GTB Standard Time'; }
else if (180 == summerOffset && 180 == winterOffset) { timeZoneHiddenField = 'E. Africa Standard Time'; }
else if (240 == summerOffset && 180 == winterOffset) { timeZoneHiddenField = 'Russian Standard Time'; }
else if (240 == summerOffset && 240 == winterOffset) { timeZoneHiddenField = 'Arabian Standard Time'; }
else if (270 == summerOffset && 210 == winterOffset) { timeZoneHiddenField = 'Iran Standard Time'; }
else if (270 == summerOffset && 270 == winterOffset) { timeZoneHiddenField = 'Afghanistan Standard Time'; }
else if (300 == summerOffset && 240 == winterOffset) { timeZoneHiddenField = 'Pakistan Standard Time'; }
else if (300 == summerOffset && 300 == winterOffset) { timeZoneHiddenField = 'West Asia Standard Time'; }
else if (330 == summerOffset && 330 == winterOffset) { timeZoneHiddenField = 'Indian Standard Time'; }
else if (345 == summerOffset && 345 == winterOffset) { timeZoneHiddenField = 'Nepal Standard Time'; }
else if (360 == summerOffset && 300 == winterOffset) { timeZoneHiddenField = 'N. Central Asia Standard Time'; }
else if (360 == summerOffset && 360 == winterOffset) { timeZoneHiddenField = 'Central Asia Standard Time'; }
else if (390 == summerOffset && 390 == winterOffset) { timeZoneHiddenField = 'Myanmar Standard Time'; }
else if (420 == summerOffset && 360 == winterOffset) { timeZoneHiddenField = 'North Asia Standard Time'; }
else if (420 == summerOffset && 420 == winterOffset) { timeZoneHiddenField = 'SE Asia Standard Time'; }
else if (480 == summerOffset && 420 == winterOffset) { timeZoneHiddenField = 'North Asia East Standard Time'; }
else if (480 == summerOffset && 480 == winterOffset) { timeZoneHiddenField = 'China Standard Time'; }
else if (540 == summerOffset && 480 == winterOffset) { timeZoneHiddenField = 'Yakutsk Standard Time'; }
else if (540 == summerOffset && 540 == winterOffset) { timeZoneHiddenField = 'Tokyo Standard Time'; }
else if (570 == summerOffset && 570 == winterOffset) { timeZoneHiddenField = 'Cen. Australia Standard Time'; }
else if (570 == summerOffset && 630 == winterOffset) { timeZoneHiddenField = 'Australia/Adelaide'; }
else if (600 == summerOffset && 540 == winterOffset) { timeZoneHiddenField = 'Asia/Yakutsk'; }
else if (600 == summerOffset && 600 == winterOffset) { timeZoneHiddenField = 'E. Australia Standard Time'; }
else if (600 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'AUS Eastern Standard Time'; }
else if (630 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'Australia/Lord_Howe'; }
else if (660 == summerOffset && 600 == winterOffset) { timeZoneHiddenField = 'Tasmania Standard Time'; }
else if (660 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'West Pacific Standard Time'; }
else if (690 == summerOffset && 690 == winterOffset) { timeZoneHiddenField = 'Central Pacific Standard Time'; }
else if (720 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'Magadan Standard Time'; }
else if (720 == summerOffset && 720 == winterOffset) { timeZoneHiddenField = 'Fiji Standard Time'; }
else if (720 == summerOffset && 780 == winterOffset) { timeZoneHiddenField = 'New Zealand Standard Time'; }
else if (765 == summerOffset && 825 == winterOffset) { timeZoneHiddenField = 'Pacific/Chatham'; }
else if (780 == summerOffset && 780 == winterOffset) { timeZoneHiddenField = 'Tonga Standard Time'; }
else if (840 == summerOffset && 840 == winterOffset) { timeZoneHiddenField = 'Pacific/Kiritimati'; }
else { timeZoneHiddenField = 'US/Pacific'; }
return timeZoneHiddenField;

}

代码被 stackoverflow 拆分错误,这就是我将它添加到 3 个部分的原因。

标签: jquery

解决方案


试试这个:你将参数从循环传递给区间函数。

你应该知道下一个循环中的变量是不同的,区间函数不知道循环中的变量。因此,您必须将它们添加到间隔函数的调用中,以便稍后调用间隔函数时,您可以使用参数。

只是一个函数作为你的例子:

function timelefttolistnewtimer1() {

    var arr = [];
    var offset = getTimezoneName();

    jQuery('.timelefttolistnewtimer1').each(function (k, v) {

        var id = jQuery(this).attr('id');

        arr["countDownDate" + id] = Date.parse(jQuery(this).data('time'));


        // Update the count down every 1 second
        arr["x" + id] = setInterval(function (id2, offset2) { // move forward the params for use them in the interval function #tipp1

            // Get today's date and time
            arr["now" + id2] = Date.parse(new Date());

            // Find the distance between now and the count down date
            arr["distance" + id2] = arr["countDownDate" + id2] - arr["now" + id2];

            // Time calculations for days, hours, minutes and seconds

            arr["seconds" + id2] = Math.floor((arr["distance" + id2] / 1000) % 60);
            arr["minutes" + id2] = Math.floor((arr["distance" + id2] / 1000 / 60) % 60);
            arr["hours" + id2] = Math.floor((arr["distance" + id2] / (1000 * 60 * 60)) % 24);
            arr["days" + id2] = Math.floor(arr["distance" + id2] / (1000 * 60 * 60 * 24));

            // Output the result in an element with id2="demo"


            jQuery("#" + id2).html(arr["days" + id2] + "d " + arr["hours" + id2] + "h "
                + arr["minutes" + id2] + "m " + arr["seconds" + id2] + "s " + offset2);


            //console.log('arr');
            //console.log(arr["seconds"+id2]);
            //console.log(offset);

            // If the count down is over, write some text 
            if (arr["distance" + id2] < 0) {
                clearInterval(arr["x" + id2]);
                jQuery("#" + id2).html("EXPIRED");
            }
        }, 1000, id, offset); // move forward the origin variables to use them in the interval function #tipp2 (function arguments id2, offset2)


    });
}

此外,如果函数被多次调用,该arr对象应该是全局可用的。timelefttolistnewtimer1问题是这些是否需要保存?


推荐阅读