2016-12-21 83 views
1

我在網上找到了代碼,我想在它上面添加啓動和重置按鈕。我希望有人能幫助我。如何將開始和重置添加到倒數計時器?

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) % 25); 
 
    return { 
 
    'total': t, 
 
    'minutes': minutes, 
 
    'seconds': seconds 
 
    }; 
 
} 
 

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

 
    function updateClock() { 
 
    var t = getTimeRemaining(endtime); 
 
    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()) + 15 * 24 * 60 * 60 * 1000); 
 
initializeClock('clockdiv', deadline);
<div id="clockdiv"> 
 
    <div> 
 
    <span class="minutes"></span> 
 
    <div class="smalltext">Minutes</div> 
 
    </div> 
 
    <div> 
 
    <span class="seconds"></span> 
 
    <div class="smalltext">Seconds</div> 
 
    </div> 
 
</div>

+0

這個問題跟Java沒有關係! Java!= Javascript,所以請正確標記您的問題 – Frakcool

+2

向我們展示您嘗試去做的事情,但這並不奏效? – kukkuz

回答

0

我覺得她是問一個按鈕如何與你的腳本進行交互。按鈕可以從您的腳本訪問方法。因此,點擊此按鈕將調用啓動功能在你的腳本
開始

http://codepen.io/jdeyrup/pen/ObGPdB
下面是如何與你的計時器做到這一點的例子。

<div id="clockdiv"> 
    <div id="days">Days</div> 
    <div id="hours">Hours</div> 
    <div id="minutes">Minutes</div> 
    <div id="seconds">Seconds</div> 
</div> 
<button type="button" onclick="start()">Start</button> 
<button type="button" onclick="stop()">Stop</button> 
<script> 
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 { days, hours, minutes, seconds, totalTime: t }; 
} 

function updateClock(endtime) { 
    var seconds = document.getElementById('seconds'); 
    var minutes = document.getElementById("minutes"); 
    var hours = document.getElementById("hours"); 
    var days = document.getElementById('days'); 
    var t = getTimeRemaining(endtime); 
    if(t.totalTime <= 0) stop(); 
    seconds.innerHTML = ('0' + t.seconds).slice(-2) + ' Seconds'; 
    minutes.innerHTML = ('0' + t.minutes).slice(-2) + ' Minutes '; 
    hours.innerHTML = ('0' + t.hours).slice(-2) + ' Hours'; 
    days.innerHTML = ('0' + t.days).slice(-2) + ' Days'; 
} 

function start() { 
    var deadline = new Date(Date.parse(new Date()) + 15 * 1000); 
    updateClock(deadline); 
    clockTimer = setInterval(function() { 
    updateClock(deadline); 
    }, 1000); 
} 

function stop() { 
    clearInterval(clockTimer); 
} 

var clockTimer; 
</script> 
+0

非常感謝,它確實有幫助:D – anna

相關問題