Skip to content

Instantly share code, notes, and snippets.

@jaikt
Created September 14, 2021 06:31
Show Gist options
  • Save jaikt/8b95176527dea7e4d927a0a494d45eba to your computer and use it in GitHub Desktop.
Save jaikt/8b95176527dea7e4d927a0a494d45eba to your computer and use it in GitHub Desktop.
How To Stop Auto Refresh Page Using JavaScript
<script>
var state = history.state || {};
var reloadCount = state.reloadCount || 0;
if (performance.navigation.type === 1) { // Reload
state.reloadCount = ++reloadCount;
history.replaceState(state, null, document.URL);
} else if (reloadCount) {
delete state.reloadCount;
reloadCount = 0;
history.replaceState(state, null, document.URL);
}
if (reloadCount >= 5) {
window.location.href = "";
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment