'Recursive function not being invoked in Chrome Extension

The function clearWatchLater() is not being invoked after the timeout expires. The code inside the setTimeout() never executes.

const clearWatchLater = () => {
    const youtubeBaseUrl = 'https://www.youtube.com';
    if (window.location.href === youtubeBaseUrl) {
        // Do Something
    } else {
        window.focus()
        window.location.href = youtubeBaseUrl;
        setTimeout(() => {
            clearWatchLater()
        }, 5000);
    }
}


Solution 1:[1]

Yes, clearWatchLater() will not be invoked.

Because When your else block gets execute you are reloading the page by using window.location.href = youtubeBaseUrl . After reloading the page Javascript call stack gets reinitialized. It will not get any previous task on its stack. SO your setTimeout is not executing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 F.Abdullah