I know the dev team is hard at work on implementing setTimeout
but in the meantime, I needed some mechanism to get regular callbacks to my application for the purposes of debugging. In case someone needs a workaround, I’ll just leave this here. Warning that I wouldn’t ship with this code.
The trick requires a url that you know will not return a response and will eventually timeout. Then, all you have to do is make a request to that url and set the network timeout to whatever value you would put in your setTimeout
.
There are of course caveats to this like it probably won’t work if you’re offline and if your timeout is greater than the standard HTTP default timeout. Still, it’s pretty handy until there’s a better solution.
Last note: this hack relies on recursion (which may be bad for the stack depending on how things are handled under the hood) but more importantly, documentRoot
seems to be a reference to memory so even though we are technically passing the same documentRoot
to each subsequent call, as you update your document, you will see documentRoot
change.
function layoutCommand(selection, documentRoot) {
// do something interesting here
// our timer starts here
const req = new XMLHttpRequest();
req.timeout = 5000; // here's our timeout
req.ontimeout = function() {
layoutCommand(selection, documentRoot)
};
req.open('GET', 'https://someknownbadurl.com', true);
req.send();
}