嗨,我對Javascript很陌生,所以請多包涵...
我正在嘗試運行一個計時器,當(dāng)檢測到鼠標(biāo)沒有移動時,它就會啟動,在計時器之后添加一個類,當(dāng)檢測到鼠標(biāo)點擊時,這個類就會被刪除。但是我似乎在網(wǎng)上找不到任何檢測不到鼠標(biāo)移動的東西。
所以現(xiàn)在我有一些東西在5秒鐘后顯示,這是我在控制臺上看到的。但是我需要在計時器運行后讓相同的元素在單擊時消失,但是我不確定應(yīng)該在哪里添加它,我想這是計時器運行后我需要的:
checkOnMeCard.classList.remove('。show me’);checkOnMeCard.classList.add('。hideMe’);
(.showMe有不透明度:1;。hideMe有不透明度:0;)
我希望這有意義。可能有助于了解我從長遠(yuǎn)來看想要做的事情就像一個& quot屏保& quot像在電腦上一樣為網(wǎng)站輸入內(nèi)容。
# # #希望這對你有所幫助:
let timeoutHandle;
const secondsMouseShouldStandStill = 5;
//we want the mousmove event to be throttled, so our resetMovementTimer is only called once every second:
document.body.addEventListener('mousemove', throttle(resetMovementTimer, 1000));
//reusable throttle function (credit to https://programmingwithmosh.com/javascript/javascript-throttle-and-debounce-patterns/)
function throttle(callback, interval) {
let enableCall = true;
return function(...args) {
if (!enableCall) return;
enableCall = false;
callback.apply(this, args);
setTimeout(() => enableCall = true, interval);
}
}
//this function cancelles the timeout when mouse movement is detected
function resetMovementTimer() {
window.clearTimeout(timeoutHandle);
timeoutHandle = window.setTimeout(function () {
executedAfterMouseStandsStill();
}, secondsMouseShouldStandStill * 1000);
}
//this is called after the mouse stands still for some time
function executedAfterMouseStandsStill() {
//do your css manipulations here...
}
最誠摯的問候, 邁克