2017-10-05 59 views
-1

意外的行为我首先在下拉列表中选择时区,当我在select中按另一个时区时,先前选定的时区为保存。moment js bug:我选择新时区时的意外行为

function run(){ 
    var timeZone = document.getElementById("selectTimezone"); 

    var i = timeZone.selectedIndex; 
    var removeParenthesis = timeZone.options[i].text.replace(/[()]/g, '') 
    var splitString = removeParenthesis.split(' ') 
    var d = new Date('Nov 1, 2017 ' + ' ' + splitString[0]) 

    var $clock = $('#clock'), 
     eventTime = moment(d.getTime()).unix(), 
     currentTime = moment(new Date().getTime()).unix(), 
     diffTime = eventTime - currentTime, 
     duration = moment.duration(diffTime * 1000, 'milliseconds'), 
     interval = 1000; 

    // if time to countdown 
    if(diffTime > 0) { 

    // Show clock 
    // $clock.show(); 


    setInterval(function(){ 

     duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds'); 
     var d = moment.duration(duration).days(), 
      h = moment.duration(duration).hours(), 
      m = moment.duration(duration).minutes(), 
      s = moment.duration(duration).seconds(); 

     d = $.trim(d).length === 1 ? '0' + d : d; 
     h = $.trim(h).length === 1 ? '0' + h : h; 
     m = $.trim(m).length === 1 ? '0' + m : m; 
     s = $.trim(s).length === 1 ? '0' + s : s; 

     // show how many hours, minutes and seconds are left 
     $('.days').text(d + 'days'); 
     $('.hours').text(h + 'hours'); 
     $('.minutes').text(m + 'minutes'); 
     $('.seconds').text(s + 'seconds'); 

    }, interval); 

    } 
} 

https://codepen.io/edward1995/pen/oGpMOq

+0

https://codepen.io/edward1995/pen/oGpMOq –

+1

请告诉我你的问题之前的时间间隔? – Peter

+1

每次你呼叫run()时,一个新的时间间隔开始尝试overrwite .days,.hours,...等。确保在开始一个新的间隔之前保存间隔并取消它。 – Peter

回答

0

清除开始一个新的

var myInterval;// Used to save previous interval if one has been started 
function run(){ 
    var timeZone = document.getElementById("selectTimezone"); 

    var i = timeZone.selectedIndex; 
    var removeParenthesis = timeZone.options[i].text.replace(/[()]/g, '') 
    var splitString = removeParenthesis.split(' ') 
    var d = new Date('Nov 1, 2017 ' + ' ' + splitString[0]) 

    var $clock = $('#clock'), 
     eventTime = moment(d.getTime()).unix(), 
     currentTime = moment(new Date().getTime()).unix(), 
     diffTime = eventTime - currentTime, 
     duration = moment.duration(diffTime * 1000, 'milliseconds'), 
     interval = 1000; 

    // if time to countdown 
    if(diffTime > 0) { 

    // Show clock 
    // $clock.show(); 

    clearInterval(myInterval); 
    myInterval = setInterval(function(){ 

     duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds'); 
     var d = moment.duration(duration).days(), 
      h = moment.duration(duration).hours(), 
      m = moment.duration(duration).minutes(), 
      s = moment.duration(duration).seconds(); 

     d = $.trim(d).length === 1 ? '0' + d : d; 
     h = $.trim(h).length === 1 ? '0' + h : h; 
     m = $.trim(m).length === 1 ? '0' + m : m; 
     s = $.trim(s).length === 1 ? '0' + s : s; 

     // show how many hours, minutes and seconds are left 
     $('.days').text(d + 'days'); 
     $('.hours').text(h + 'hours'); 
     $('.minutes').text(m + 'minutes'); 
     $('.seconds').text(s + 'seconds'); 

    }, interval); 

    } 
} 
+0

不客气,请将答案标记为已接受 – Peter