Last active
October 26, 2023 15:31
-
-
Save moimikey/07501755bcba3aa409b0 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Michael Scott Hertzberg <mshertzberg@gmail.com> (http://hertzber.gs) | |
* | |
* recursively check for set localStorage value every 100ms. | |
* this will also safely clear the timeout once found. | |
* can be used callback style or directly. | |
* functionally pure. | |
*/ | |
function waitForLocalStorage(key, cb, timer) { | |
if (!localStorage.getItem(key)) return (timer = setTimeout(waitForLocalStorage.bind(null, key, cb), 100)) | |
clearTimeout(timer) | |
if (typeof cb !== 'function') return localStorage.getItem(key) | |
return cb(localStorage.getItem(key)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dragGH102 thanks! good catch! gist has been updated.