我將此javascript用作div的倒數計時器,同時檢測鼠標閒置。如何在沒有鼠標移動的情況下重定向並顯示當前計時器的計數
var timer = null;
setInterval(function() {
var div = document.querySelector("#counter");
var count = div.textContent * 1 - 1;
div.textContent = count;
if (count == 0) {
window.location.href="https://example.com";
}
}, 1000);
function goAway() {
clearTimeout(timer);
timer = setTimeout(function() {
window.location.href="https://example.com";
}, 5000);
}
window.addEventListener('mousemove', goAway, true);
goAway();
如果用戶超過5秒沒有鼠標移動,我希望他被重定向到另一個頁面。 example.com在這種情況下。這部分工作。不過,我也打算讓一個div的權利顯示倒數被重定向,並在.mousemove事件的情況下消失。我似乎無法讓他們兩人工作。
是否有可能?
請解釋一下你的代碼是做錯了。在我們能夠幫助解決問題之前,我們需要知道哪些方面無法解決。 – Soviut