I have a function that I need to be called every time when my screen width is 1240px. What I mean is, for example, if I resize from 1300px or more to 1200px or less and as soon as it passes through 1240px my function should be called once. Same goes to, for example, if I resize from 1200px or less to 1300px or more and as soon as it passes through 1240px my function is being called once again.
that's what I tried:
function toggle() {
toggle_time = setTimeout('toggle()', 1);
if (document.body.offsetWidth<=1240) {
myFunction();
}
if (document.body.offsetWidth>=1240) {
myFunction();
}
}
But it will call my function always and every millisecond what is wrong. Any answer is appreciated. Thanks !