2017-10-05 6 views
-1

予期しない動作選択したタイムゾーンを前回選択したときにドロップダウンでタイムゾーンを最初に選択し、前回選択したタイムゾーンを保存します。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()を呼び出すたびに、新しい間隔で.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

関連する問題