2012-09-19 34 views
7

मुझे यह फ़ंक्शन मिला है जो इस प्रारूप पर टाइमर शुरू करता है 00:00:00 जब भी मैं एक बटन पर क्लिक करता हूं। लेकिन मुझे नहीं पता कि फ़ंक्शन कैसे शुरू करें और रोकें। मुझे कुछ स्निपेट मिले हैं जो मैंने सोचा था कि मददगार हो सकता है लेकिन मैं उन कामों को नहीं कर सका। मैं जेएस में वस्तुओं का उपयोग करने के लिए नया हूँ।मैं टाइमर को कैसे रोकूं और फिर से शुरू करूं?

function clock() { 
    var pauseObj = new Object(); 

    var totalSeconds = 0; 
    var delay = setInterval(setTime, 1000); 

    function setTime() { 
    var ctr; 
    $(".icon-play").each(function() { 
     if ($(this).parent().hasClass('hide')) ctr = ($(this).attr('id')).split('_'); 
    }); 

    ++totalSeconds; 
    $("#hour_" + ctr[1]).text(pad(Math.floor(totalSeconds/3600))); 
    $("#min_" + ctr[1]).text(pad(Math.floor((totalSeconds/60) % 60))); 
    $("#sec_" + ctr[1]).text(pad(parseInt(totalSeconds % 60))); 
    } 
} 

पैड() सिर्फ प्रमुख जोड़ता शून्य

उत्तर

18

मुझे लगता है कि यदि आप घड़ी की वस्तु बनायेंगे तो बेहतर होगा। कोड देखें (डेमो देखें: http://jsfiddle.net/f9X6J/):

var Clock = { 
    totalSeconds: 0, 

    start: function() { 
    var self = this; 

    this.interval = setInterval(function() { 
     self.totalSeconds += 1; 

     $("#hour").text(Math.floor(self.totalSeconds/3600)); 
     $("#min").text(Math.floor(self.totalSeconds/60 % 60)); 
     $("#sec").text(parseInt(self.totalSeconds % 60)); 
    }, 1000); 
    }, 

    pause: function() { 
    clearInterval(this.interval); 
    delete this.interval; 
    }, 

    resume: function() { 
    if (!this.interval) this.start(); 
    } 
}; 

Clock.start(); 

$('#pauseButton').click(function() { Clock.pause(); }); 
$('#resumeButton').click(function() { Clock.resume(); }); 
+0

धन्यवाद @Speransky। इस दृष्टिकोण के साथ, मैं एक बटन पर रोक या फिर से शुरू कैसे कर सकता हूं? – esandrkwn

+1

फिर से धन्यवाद। मुझे यह मेरे लिए काम कर रहा है। – esandrkwn

+0

मैं स्वयं के उपयोग को कम करने में सक्षम नहीं हूं ... अलग-अलग स्थानों में यह अलग-अलग अर्थ है ... मैं थोड़ा उलझन में हूं .. क्या आप कृपया समझा सकते हैं। इस http://jsfiddle.net के लिए एक अलग दृष्टिकोण है/wMJuQ/ –

0

उपयोग window.clearInterval दोहराया कार्रवाई जो setInterval() का उपयोग कर स्थापित किया गया था रद्द करने के लिए।

clearInterval(delay); 
+0

आप इसे कैसे setTime (के बाद से पुन: प्रारंभ होगा) समारोह घड़ी के अंदर है()? – HeatfanJohn

+0

@HatfanJohn यह 'रोक' के अर्थ पर निर्भर करता है, अगर आप केवल प्रदर्शन को रोकना चाहते हैं और टाइमर जारी रखना चाहते हैं, तो 'clearInterval' का उपयोग न करें, आपको केवल HTML सामग्री के प्रदर्शन को रीफ्रेश करना बंद करना होगा। – xdazz

1

बस अंतराल को साफ़ करना काम नहीं करेगा, क्योंकि कुल सेकेंड बढ़े नहीं जाएंगे। मैं एक ध्वज स्थापित करता हूं जो निर्धारित करता है कि घड़ी रुक गई है या नहीं।

यह ध्वज बस रोज़() को कॉल करने पर सेट किया जाएगा या फिर से शुरू() पर सेट किया जाएगा। मैंने कुल सेकेंड को 'टिक' टाइमआउट में बढ़ा दिया जो हमेशा चल रहा होगा, यहां तक ​​कि रोके जाने पर भी (ताकि हम उस समय का ट्रैक रख सकें जब हम फिर से शुरू करते हैं)।

इसलिए टिक फ़ंक्शन केवल तभी अपडेट होगा जब घड़ी रोका नहीं गया हो।

function clock() 
{ 
    var pauseObj = new Object(); 

    var totalSeconds = 0; 
    var isPaused = false; 
    var delay = setInterval(tick, 1000); 

    function pause() 
    { 
     isPaused = true; 
    } 

    function resume() 
    { 
     isPaused = false; 
    } 

    function setTime() 
    { 
     var ctr; 
     $(".icon-play").each(function(){ 
      if($(this).parent().hasClass('hide')) 
       ctr = ($(this).attr('id')).split('_'); 
     }); 

     $("#hour_" + ctr[1]).text(pad(Math.floor(totalSeconds/3600))); 
     $("#min_" + ctr[1]).text(pad(Math.floor((totalSeconds/60)%60))); 
     $("#sec_" + ctr[1]).text(pad(parseInt(totalSeconds%60))); 
    } 

    function tick() 
    { 
     ++totalSeconds; 

     if (!isPaused) 
      setTime(); 
    } 
} 
+0

धन्यवाद, मैं इसे तुरंत बाहर करूँगा – esandrkwn

0
<html> 
    <head><title>Timer</title> 
    <script> 

    //Evaluate the expression at the specified interval using setInterval() 
    var a = setInterval(function(){disp()},1000); 

    //Write the display() for timer 
    function disp() 
    { 
     var x = new Date(); 

     //locale is used to return the Date object as string 
     x= x.toLocaleTimeString(); 

     //Get the element by ID in disp() 
     document.getElementById("x").innerHTML=x; 
    } 
    function stop() 
    { 
     //clearInterval() is used to pause the timer 
     clearInterval(a); 
    } 
    function start() 
    {  
     //setInterval() is used to resume the timer 
     a=setInterval(function(){disp()},1000); 
    } 

    </script> 
    </head> 
    <body> 
    <p id = "x"></p> 
    <button onclick = "stop()"> Pause </button> 
    <button onclick = "start()"> Resume </button> 
    </body> 
    </html> 
+0

कृपया, अपने कोड में कुछ टिप्पणियां जोड़ें – kvorobiev