Skip to content

Instantly share code, notes, and snippets.

@acron0
Last active September 26, 2024 13:26
Show Gist options
  • Save acron0/b237b2991a63cf7e4eefe121d10bde68 to your computer and use it in GitHub Desktop.
Save acron0/b237b2991a63cf7e4eefe121d10bde68 to your computer and use it in GitHub Desktop.
GhostInspector "wait until path"
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