2017-07-22 27 views
0

我正在使用此功能在15秒後自動點擊按鈕。問題是用戶在選項運行後沒有離開頁面,並且可能會在同一頁面上重新運行,但定時器仍在繼續。事實上,即使我自己動手,計時器也會繼續。重置Javascript中的變量執行?

<script type="text/javascript"> 
time = 15; 
interval = setInterval(function() { 
    time--; 
    document.getElementById('Label1').innerHTML = "You must choose in " + time + " seconds" 
    if (time == 0) { 
     // stop timer 
     clearInterval(interval); 
     // click 
     document.getElementById('thebutton').click();  
    } 
}, 1000) 

</script> 

所以這個腳本應該在十五秒跑完計時器和「新聞」的「thebutton」,然後定時器應停止計數,再重置,直到運行。如果在15秒前手動按下該按鈕,它仍然應該重置。

<input type='submit' id='thebutton' value='Done'></input> 

希望這是明確的。我仍然是新的和學習。

+0

什麼是標籤1? – Nihal

+0

目前還不清楚你在問什麼。也許你可以添加一些你想要做的解釋。 – Nisarg

+0

什麼行動導致計時器啓動? – H77

回答

1

設置基準時間,然後將其重置爲該基準時間。

<script type="text/javascript"> 
time = 15; 
baseTime = 15; 
interval = setInterval(function() { 
    time--; 
    document.getElementById('Label1').innerHTML = "You must choose in " + time + " seconds" 
    if (time == 0) { 
     // stop timer 
     clearInterval(interval); 
     // click 
     document.getElementById('thebutton').click(); 
     time = baseTime; 
     return false; 


    } 
}, 1000) 

</script> 
+0

這實際上是一半的工作,這很接近。它會將時間重置爲15,但倒計時從不停止,因此它重置爲15,但在不重新執行的情況下重新開始倒計時。 – Chris

+0

我剛做了一個編輯。我加了'return false;'應該停止計時器。讓我知道如果它不。 – Difster

0

如果你只想在15秒後點擊一次按鈕,那麼你應該使用setTimeout()函數而不是setInterval()。

然後,如果您不想在用戶單擊按鈕時發生自動點擊,則需要將onClick處理程序添加到調用clearTimeout()的按鈕。

0

我想你想標籤更新爲秒數倒數?目前還不清楚定時器是如何啓動的。檢查下面的代碼,看看它是否符合你的期望。

var time, interval; 
 

 
function stopTimer() { 
 
    if (interval) { 
 
    clearInterval(interval); 
 
    interval = null; 
 
    } 
 
    
 
    time = 15; 
 
} 
 

 
function timerAction() { 
 
    $('#lblStatus').text("You must choose in " + time + " seconds"); 
 

 
    if (time-- <= 0) { 
 
    stopTimer(); 
 
    console.log("done!"); 
 
    $("#btnStop").click(); 
 
    } 
 
} 
 

 
function startTimer() { 
 
    stopTimer(); 
 
    timerAction(); 
 
    interval = setInterval(timerAction, 1000); 
 
} 
 

 
$("#btnStart").click(function() { 
 
    startTimer(); 
 
}); 
 

 
$("#btnStop").click(function() { 
 
    stopTimer(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<span id=lblStatus></span> 
 
<button id='btnStart'>Reset/Start</button> 
 
<button id='btnStop'>Stop</button>

0

如果你想只運行一次,你可以使用setTimeout函數

setTimeout(your code, 15000); 
1

我看了一下代碼,最重要的事情,我想你應該看看這個按鈕沒有「onclick」功能。 這意味着單擊按鈕不會執行任何操作,因爲您沒有在其中單擊某個功能時會執行某些操作。

我寫了一些代碼,我希望幫助:

let time = 15; 
const label = document.getElementById("Label1"); 
const button = document.getElementById("thebutton"); 

const getText =() => `You must choose in ${time} seconds`; 

const interval = setInterval(() => { 
    time--; 
    label.innerHTML = getText(); 
    if (time === 0) { 
    // stop timer 
    clearInterval(interval); 
    // click 
    button.click(); 
    } 
}, 1000); 

const stopTime =() => { 
    clearInterval(interval); 
    time = 15; 
    label.innerHTML = getText(); 
}; 

而在你的HTML是這樣的:

<input type='submit' id='thebutton' value='Done' onclick="stopTime()" /> 

最後我做了一個小的視頻,我遍歷代碼,它可能也是有用的:https://youtu.be/ZYS9AcxO3d4

祝您有美好的一天!