I'm checking for three window events in my script:
window.onload = updateData;
window.onresize = updateData;
window.onscroll = updateData;
How could I optimize my code and write this in one line? I tried:
window.onload || window.onresize || window.onscroll = updateData;
but it doesn't work.
window.onload = window.onresize = window.onscroll = updateData;
If this was a normal variable assignation such as:
a = 1 this would return 1,
so in b = a = 1 , the b var receives the value 1 from the previous assignation and the current on returns 1 too as result.
On the other hand I would suggest that the updateData function would implement some race condition control to prevent that routine to be running simultaneously.
If you have a lot of properties to assign, then
['onload', 'onresize', 'onscroll'].forEach(function(x){window[x] = updateData})
will eventually be shorter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With