सेटटाइमआउट रीसेट करना


160

मेरे पास निम्नलिखित हैं:

window.setTimeout(function() {
    window.location.href = 'file.php';
}, 115000);

मैं .click फ़ंक्शन के माध्यम से, काउंटडाउन के माध्यम से काउंटर मिडवे को कैसे रीसेट कर सकता हूं?


आप संभवतः मौजूदा डेबिट कार्यान्वयन का उपयोग कर सकते हैं।
अट्टुरसम्स

जवाबों:


263

आप उस टाइमआउट के लिए एक संदर्भ स्टोर कर सकते हैं, और फिर clearTimeoutउस संदर्भ पर कॉल कर सकते हैं।

// in the example above, assign the result
var timeoutHandle = window.setTimeout(...);

// in your click function, call clearTimeout
window.clearTimeout(timeoutHandle);

// then call setTimeout again to reset the timer
timeoutHandle = window.setTimeout(...);

2
इतना अजीब है कि .clear()टाइमआउट ऑब्जेक्ट पर ही नहीं है।
Automatico

16
@ Cort3z ऐसा इसलिए है क्योंकि window.setTimeoutएक नंबर (टाइमर की आईडी) और एक "टाइमआउट ऑब्जेक्ट" नहीं है।
डैन ओ

2
हां @ डानो: अजीब है कि सेटटाइमआउट टाइमआउट ऑब्जेक्ट वापस नहीं करता है। एक NodeJS संदर्भ में यह करता है।
की ज्यू

25

ClearTimeout () और सेटटाइमआउट के संदर्भ को फीड करें, जो एक नंबर होगा। फिर इसे फिर से लागू करें:

var initial;

function invocation() {
    alert('invoked')
    initial = window.setTimeout( 
    function() {
        document.body.style.backgroundColor = 'black'
    }, 5000);
}

invocation();

document.body.onclick = function() {
    alert('stopped')
    clearTimeout( initial )
    // re-invoke invocation()
}

इस उदाहरण में, यदि आप 5 सेकंड में बॉडी एलिमेंट पर क्लिक नहीं करते हैं तो बैकग्राउंड का रंग काला हो जाएगा।

संदर्भ:

नोट: setTimeout और ClearTimeout ECMAScript देशी तरीके नहीं हैं, बल्कि वैश्विक विंडो नामस्थान के जावास्क्रिप्ट तरीके हैं।


9

आपको टाइमआउट "टाइमर" को याद रखना होगा, इसे रद्द करना होगा, फिर इसे फिर से शुरू करना होगा:

g_timer = null;

$(document).ready(function() {
    startTimer();
});

function startTimer() {
    g_timer = window.setTimeout(function() {
        window.location.href = 'file.php';
    }, 115000);
}

function onClick() {
    clearTimeout(g_timer);
    startTimer();
}

1
दिलचस्प है कि इस विकल्प को नजरअंदाज कर दिया गया है। अन्य सभी को टाइमर आंतरिक फ़ंक्शन को डुप्लिकेट करने की आवश्यकता लगती है, जो कि दो बार बनाए रखने के लिए कोड की कुछ पंक्तियों से अधिक होने पर सूखे और दर्दनाक से कम है ...
brianlmerritt

8
var myTimer = setTimeout(..., 115000);
something.click(function () {
    clearTimeout(myTimer);
    myTimer = setTimeout(..., 115000);
}); 

उन लाइनों के साथ कुछ!


2

यह टाइमर 30 सेकंड के बाद "हैलो" अलर्टबॉक्स में आग लगा देगा। हालाँकि, हर बार जब आप रीसेट टाइमर बटन पर क्लिक करते हैं, तो यह टाइमरहैंडल को साफ़ करता है और फिर से सेट करता है। एक बार जब यह निकाल दिया जाता है, तो खेल समाप्त हो जाता है।

<script type="text/javascript">
    var timerHandle = setTimeout("alert('Hello')",3000);
    function resetTimer() {
        window.clearTimeout(timerHandle);
        timerHandle = setTimeout("alert('Hello')",3000);
    }
</script>

<body>
    <button onclick="resetTimer()">Reset Timer</button>
</body>

2
var redirectionDelay;
function startRedirectionDelay(){
    redirectionDelay = setTimeout(redirect, 115000);
}
function resetRedirectionDelay(){
    clearTimeout(redirectionDelay);
}

function redirect(){
    location.href = 'file.php';
}

// in your click >> fire those
resetRedirectionDelay();
startRedirectionDelay();

यहाँ क्या वास्तव में http://jsfiddle.net/ppjrnd2L/ पर जा रहा है के लिए एक विस्तृत उदाहरण है


1
$(function() {

    (function(){

        var pthis = this;
        this.mseg = 115000;
        this.href = 'file.php'

        this.setTimer = function() { 
            return (window.setTimeout( function() {window.location.href = this.href;}, this.mseg));
        };
        this.timer = pthis.setTimer();

        this.clear = function(ref) { clearTimeout(ref.timer); ref.setTimer(); };
        $(window.document).click( function(){pthis.clear.apply(pthis, [pthis])} );

    })();

});

1

टाइमर को रीसेट करने के लिए, आपको टाइमर वेरिएबल को सेट और क्लियर करना होगा

$time_out_handle = 0;
window.clearTimeout($time_out_handle);
$time_out_handle = window.setTimeout( function(){---}, 60000 );

25
आर्ग, मैं जावास्क्रिप्ट में php शैली चर नाम का उपयोग करने के खिलाफ सलाह देता हूं। हाँ, यह काम करेगा, लेकिन मेरे भगवान, यह भ्रामक है।
16

0

मुझे पता है कि यह एक पुराना धागा है लेकिन मैं आज इसे लेकर आया हूं

var timer       = []; //creates a empty array called timer to store timer instances
var afterTimer = function(timerName, interval, callback){
    window.clearTimeout(timer[timerName]); //clear the named timer if exists
    timer[timerName] = window.setTimeout(function(){ //creates a new named timer 
        callback(); //executes your callback code after timer finished
    },interval); //sets the timer timer
}

और आप का उपयोग कर आह्वान

afterTimer('<timername>string', <interval in milliseconds>int, function(){
   your code here
});
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.