Last active
September 26, 2024 13:26
-
-
Save acron0/b237b2991a63cf7e4eefe121d10bde68 to your computer and use it in GitHub Desktop.
GhostInspector "wait until path"
This file contains hidden or 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 waitForPath(resolve, reject, targetPath, maxWaitTime = 60000) { | |
var startTime = Date.now(); // Record the start time | |
var checkPath = setInterval(function() { | |
var elapsedTime = Date.now() - startTime; | |
if (window.location.pathname === targetPath) { | |
clearInterval(checkPath); // Stop the loop when the condition is met | |
resolve(`Path is now ${targetPath}`); // Resolve the promise with the target path | |
} | |
// Check if the maximum wait time has been exceeded | |
if (elapsedTime >= maxWaitTime) { | |
clearInterval(checkPath); // Stop checking | |
reject(`Max wait time exceeded while waiting for path to become ${targetPath}`); // Reject the promise with an error message | |
} | |
}, 100); // Check every 100 milliseconds | |
} | |
return new Promise(function(resolve, reject) { | |
try { | |
waitForPath(resolve, reject, "/dashboard"); | |
} catch (error) { | |
console.error(error) | |
reject(error) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment