Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- /**
- * `callWhenHrefChanges(callback)`
- *
- * Schedules a callback to be called whenever the browser's URL
- * (`window.location.href`) changes.
- *
- */
- let callWhenHrefChanges = (() => {
- // =========================================================================
- // Stores previous href location.
- let prev = '';
- // Stores functions to be called
- let functions = new Array();
- // =========================================================================
- /** Checks if the URL has changed. Updates the `prev` if so. */
- function hasHrefChanged() {
- let hasChanged = false;
- const href = window.location.href;
- if ( href != prev ) {
- prev = href;
- hasChanged = true;
- }
- return hasChanged;
- }
- /** Call every function scheduled to be called on URL changes. */
- function callFunctions() {
- functions.forEach( f => f() );
- }
- /** Schedule a function to be called when the browsers URL changes. */
- function callWhenHrefChanges(callback) {
- functions.push(callback);
- }
- // == Main Async Loop ======================================================
- setInterval(() => {
- if ( hasHrefChanged() ) {
- callFunctions();
- }
- }, 250);
- // =========================================================================
- return callWhenHrefChanges;
- })();
Add Comment
Please, Sign In to add comment