I think the problem is these lines:
win.width = 500
win.height = 500
The onResize() event will run as soon as the size of the window changes and you type win.width = 500 and win.height = 500 it acts as if the size of the window has changed every second even if it doesn't actually change.