要查看該問題,請單擊https://jsfiddle.net/kjz1myL2/16/,當淡入淡出完成時,將鼠標從按鈕移至按鈕,確實很快。JavaScript:如何防止用戶崩潰setInterval/setTimeout事件?
有沒有辦法可以防止事件在崩潰時崩潰或重置?我做了一個與setInterval
發生的JavaScript事件。這有助於淡化。這是代碼中非常重要的部分,但它會讓SUPER EASILY更容易崩潰!如果用戶在兩個元素之間切換太快,則會使setInterval
崩潰。我想製作一個鼠標暫停事件,它會停止用戶的鼠標或鼠標慢速事件,這會將鼠標速度降至最低速度,直到setInterval
結束,但人們聲稱從用戶處接受控制非常危險非常顯着的原因。沒有探索這個選擇,我決定來到這裏,並尋求一個更加用戶友好的解決方案來防止崩潰的發生。我有這樣的代碼(代碼的一部分)[校正]:
的JavaScript
window.addEventListener("load", TourFunction);
var TourFadeInEvent;
var TourYesMouseEnterTimeEvent, TourNoMouseEnterTimeEvent;
var TourYesMouseEnterEvent, TourNoMouseEnterEvent;
var TourYesMouseLeaveEvent, TourNoMouseLeaveEvent;
Steps = 3;
TourFunction()
function TourFunction(){
if (Steps == 3){
Opacity = 0;
TourFadeInEvent = window.setInterval(TourFadeIn, 20);
}else{
TourYesMouseEnterEvent = TourInputYes.addEventListener("mouseenter", TourYesMouseEnter);
TourNoMouseEnterEvent = TourInputNo.addEventListener("mouseenter", TourNoMouseEnter);
}
function TourFadeIn(){
if (Opacity <= 0 || Opacity < 1){
Opacity = Opacity + .01;
TourContainer.style.opacity = Opacity;
}else{
clearInterval(TourFadeInEvent);
Steps += 1;
TourFunction();
return Steps;
}
}
function TourYesMouseEnter(){
TourYesMouseEnterTimeEvent = window.setInterval(TourYesMouseEnterTime, 10);
function TourYesMouseEnterTime(){
if (YesfgcR < 255){
YesfgcR += 5;
YesfgColor = "rgb(" + YesfgcR + ", " + YesfgcG + ", " + YesfgcB + ")";
TourInputYes.style.color = YesfgColor;
}if (YesbcR < 255){
YesbcR += 5;
YesbColor = "rgb(" + YesbcR + ", " + YesbcG + ", " + YesbcB + ")";
TourInputYes.style.borderColor = YesbColor;
}if (NofgcR > 0){
NofgcR = 0;
NofgColor = "rgb(" + NofgcR + ", " + NofgcG + ", " + NofgcB + ")";
TourInputNo.style.color = NofgColor;
}if (NobcR > 0){
NobcR = 0;
NobColor = "rgb(" + NobcR + ", " + NobcG + ", " + NobcB + ")";
TourInputNo.style.borderColor = NobColor;
}if (YesfgcR >= 255 && YesbcR >= 255 && NofgcR == 0 && NobcR == 0){
clearInterval(TourYesMouseEnterTimeEvent);
return YesfgcR, YesbcR, YesfgColor, YesbColor;
}
}
}
function TourNoMouseEnter(){
TourNoMouseEnterTimeEvent = window.setInterval(TourNoMouseEnterTime, 10);
function TourNoMouseEnterTime(){
if (NofgcR < 255){
NofgcR += 5;
NofgColor = "rgb(" + NofgcR + ", " + NofgcG + ", " + NofgcB + ")";
TourInputNo.style.color = NofgColor;
}if (NobcR < 255){
NobcR += 5;
NobColor = "rgb(" + NobcR + ", " + NobcG + ", " + NobcB + ")";
TourInputNo.style.borderColor = NobColor;
}if (YesfgcR > 0){
YesfgcR = 0;
YesfgColor = "rgb(" + YesfgcR + ", " + YesfgcG + ", " + YesfgcB + ")";
TourInputYes.style.color = YesfgColor;
}if (YesbcR > 0){
YesbcR = 0;
YesbColor = "rgb(" + YesbcR + ", " + YesbcG + ", " + YesbcB + ")";
TourInputYes.style.borderColor = YesbColor;
}if (NofgcR >= 255 && NobcR >= 255 && YesfgcR == 0 && YesbcR == 0){
clearInterval(TourNoMouseEnterTimeEvent);
return NofgcR, NobcR, NofgColor, NobColor;
}
}
}
}
的代碼運行正常,如果你不從每個按鈕過快切換,但如果你去快,它會崩潰。我一直在經歷這些,並沒有得到答案。請給我一些工作。
我首先想到的是,它可能會更好地工作,如果你使用的變量來保存您的間隔IDS('TourYesMouseEnterTimeEvent1',等等)是在鼠標事件處理程序內而不是在外部(全局?)範圍內定義的。我認爲你擁有它的方式,一旦鼠標移過按鈕兩次,你已經失去了對第一次間隔的引用,所以它永遠運行。或者,也許你應該在開始新的區間之前清除之前的區間。 – nnnnnn
@nnnnnn你如何建議我清除它們? –
那麼你可以在調用'setInterval()'之前立即調用'clearInterval()'。 (我並不是說這樣可以解決所有問題,只是至少可以改進某些方面。)您還可以查看[「debounce」機制](https://davidwalsh.name/javascript -debounce-function)爲您的鼠標事件處理程序。 – nnnnnn