I am trying to make a worker that will run automatically and constantly check the browser's width, but, I am getting this error: worker.js:1 Uncaught ReferenceError: window is not defined at worker.js:1:18
This is the code:
var intervalID = window.setInterval(myCallback, 500);
function myCallback() {
var nav = document.getElementById("navigation-bar");
if (window.screen.width >= 1200) {
console.log(window.screen.width, 'Stay In desktop version');
}
else {
console.log(window.screen.width, 'Switch to mobile version');
}
}
I saw this issue and had questions about it. Although This issue for some people was solved, their solutions are not helpful in my case.