2017-08-08 68 views
0

嗨,我已經安裝了該插件有一個倒數計時器有:的jQuery秒轉換爲這種格式

function getTimeRemaining(endtime) { 
var t = Date.parse(endtime) - Date.parse(new Date()); 
     var seconds = Math.floor((t/1000) % 60); 
     var minutes = Math.floor((t/1000/60) % 60); 
     var hours = Math.floor((t/(1000 * 60 * 60)) % 24); 
     var days = Math.floor(t/(1000 * 60 * 60 * 24)); 
     return { 
     'total': t, 
     'days': days, 
     'hours': hours, 
     'minutes': minutes, 
     'seconds': seconds 
     }; 
    } 

    function initializeClock(id, endtime) { 
     var clock = document.getElementById(id); 
     var daysSpan = clock.querySelector('.days'); 
     var hoursSpan = clock.querySelector('.hours'); 
     var minutesSpan = clock.querySelector('.minutes'); 
     var secondsSpan = clock.querySelector('.seconds'); 

     function updateClock() { 
     var t = getTimeRemaining(endtime); 
     daysSpan.innerHTML = t.days; 
     hoursSpan.innerHTML = ('0' + t.hours).slice(-2); 
     minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); 
     secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); 

     if (t.total <= 0) { 
      clearInterval(timeinterval); 
     } 
     } 
     updateClock(); 
     var timeinterval = setInterval(updateClock, 1000); 
    } 

    var deadline = new Date(Date.parse(new Date()) + 1 * 60 * 60 * 1000); 
    initializeClock('clockdiv', deadline); 

我有它運行基於秒的倒計時代碼..

所以我想從上面的代碼格式寫我的代碼,在幾秒鐘內到截止日期

var gdtesttime = 00:15:00;   //hh:mm:ss 
var a = gdtesttime.split(':');  // split it at the colons 
var gdseconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 ; //seconds 

我需要在這2個主要行的幫助:

var deadline = new Date(Date.parse(new Date()) + 1 * 60 * 60 * 1000);

2.var t = Date.parse(endtime) - Date.parse(new Date());

這是我的HTML:

<div id="clockdiv" class="pull-right"> 
    <span><img src="assets/images/timer.png" /></span> 
    <div class="disp-none"> 
     <span class="days"></span> 
     <div class="smalltext">Days</div> 
    </div> 
    <div> 
     <span class="hours"></span><span>:</span> 
    </div> 
    <div> 
     <span class="minutes"></span><span>:</span> 
    </div> 
    <div> 
     <span class="seconds"></span> 
    </div> 
</div> 

JS提琴:https://jsfiddle.net/k4b81x8c/

+0

您可以創建小提琴? –

+0

它很好,你可以用JSFiddle創建 – Minesh

+0

https://jsfiddle.net/k4b81x8c/這裏是倒計時計時器@MuhammadAkberKhan –

回答

1

你有JS錯誤,如果你添加var gdtesttime = 00:15:00;你得給這次"'報價。在轉換gdseconds時也會應用* 1000,最後將其添加到死線。因此,更新腳本:

<script type="text/javascript"> 
    function getTimeRemaining(endtime) { 
var t = Date.parse(endtime) - Date.parse(new Date()); 
     var seconds = Math.floor((t/1000) % 60); 
     var minutes = Math.floor((t/1000/60) % 60); 
     var hours = Math.floor((t/(1000 * 60 * 60)) % 24); 
     var days = Math.floor(t/(1000 * 60 * 60 * 24)); 
     return { 
     'total': t, 
     'days': days, 
     'hours': hours, 
     'minutes': minutes, 
     'seconds': seconds 
     }; 
    } 

    function initializeClock(id, endtime) { 
     var clock = document.getElementById(id); 
     var daysSpan = clock.querySelector('.days'); 
     var hoursSpan = clock.querySelector('.hours'); 
     var minutesSpan = clock.querySelector('.minutes'); 
     var secondsSpan = clock.querySelector('.seconds'); 

     function updateClock() { 
     var t = getTimeRemaining(endtime); 
     daysSpan.innerHTML = t.days; 
     hoursSpan.innerHTML = ('0' + t.hours).slice(-2); 
     minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); 
     secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); 

     if (t.total <= 0) { 
      clearInterval(timeinterval); 
     } 
     } 
     updateClock(); 
     var timeinterval = setInterval(updateClock, 1000); 
    } 
    var gdtesttime = "00:15:00";   //hh:mm:ss 
    var a = gdtesttime.split(':');  // split it at the colons 
    var gdseconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 * 1000; //miliseconds 
    var deadline = new Date(Date.parse(new Date()) + gdseconds); 
    initializeClock('clockdiv', deadline); 
</script> 

檢查JS提琴:

https://jsfiddle.net/k4b81x8c/1/

+0

我想調用一個函數,定時器變爲零,我可以在那裏寫這個函數。 @ B-德賽 –

相關問題