Skip to content

Instantly share code, notes, and snippets.

@mrandrewmills
Created July 5, 2021 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrandrewmills/0878e3f8066e844fdec39e5f90248dd2 to your computer and use it in GitHub Desktop.
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.
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;
}
@mrandrewmills
Copy link
Author

mrandrewmills commented Jul 5, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment