Created
July 5, 2021 15:51
-
-
Save mrandrewmills/0878e3f8066e844fdec39e5f90248dd2 to your computer and use it in GitHub Desktop.
JS object for toggling document.title text between current value and a temporary one at a specified interval. Also a method to stop/clear.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function titleToggle( currTitle, tmpTitle, msInterval){ | |
var currTitle = currTitle; | |
var tmpTitle = tmpTitle; | |
var msInterval = msInterval; | |
var timerID = -1; | |
this.stop = function() { | |
if ( timerID != -1) { | |
clearInterval(timerID); | |
document.title = currTitle; | |
} | |
} | |
this.go = function() { | |
timerID = setInterval( switcheroo, msInterval, currTitle, tmpTitle); | |
} | |
var switcheroo = function(currTitle, tmpTitle){ | |
if (document.title == currTitle) { | |
document.title = tmpTitle; | |
} | |
else { | |
document.title = currTitle; | |
} | |
} | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Although I dislike the purpose of this snippet, I do like how declaring the stop and go methods as part of this while not doing the same for the switcheroo function simulates the public/private scope of functions.
For additional context, you might also want to check out https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API.