我正在使用一个函数,该函数使用以下代码更改标题,并且只要窗口处于焦点上,它就必须停止更改。
//Set to true on first load
var window_focus = true;
$(window).focus(function () {
return window_focus = true;
})
.blur(function () {
return window_focus = false;
});
// THE CHANGE FUNCTION
function doHighlightNow (){
var highlightTimer = null;
var oldTitle = document.title;
function doHighlight() {
if (window_focus){
stopHighlight();
}
var doBlink = function() {
document.title = "Title one"
setTimeout(function(){
document.title = "Title two";
}, 1000);
}
doBlink();
}
function stopHighlight() {
document.title = 'stopped';
clearInterval(highlightTimer);
}
highlightTimer = setInterval(function(){doHighlight(nickname) }, 2500);
}
现在,这将在每次触发
doHightlightNow()
且windows_focus
不是true
时更改标题,否则它将清除间隔。现在,我想让它再次在窗口再次聚焦时立即触发
stopHighlight()
,如果触发了doHighlightNow()
,那么对此的最佳解决方案是什么。我现在一定是这样,
$(window).on("click", "focus",function(){ trigger stopHighlightnow();});
但是我不知道该怎么提供,我希望有人能帮助我。
最佳答案
你能做的是
var highlightTimer = null; // Create it outside the function
// So you can stop it outside the function
function doHighlightNow (){
var oldTitle = document.title;
function doHighlight() {
//No need for this inside the function
//if (window_focus){
//stopHighlight();
//}
var doBlink = function() {
document.title = "Title one"
setTimeout(function(){
document.title = "Title two";
}, 1000);
}
doBlink();
}
// Neither need this anymore inside the function
//function stopHighlight() {
//document.title = 'stopped';
//clearInterval(highlightTimer);
//}
highlightTimer = setInterval(function(){doHighlight(nickname) }, 2500);
}
$(window).on("focus", function(){
if (window_focus){
document.title = 'stopped';
clearInterval(highlightTimer);
}
});
关于javascript - 高亮显示新事件标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29619996/