document body style height 300pt let div document createElement div document body appendChild div div style position absolute div style borderRadius 50 div style border 1pt solid blue div style width 50px div style height 50px div pos x 0 y 0 div vel x 1 y 1 div set function x y div style left x px div style top y px div integrate function t div pos x div vel x t div pos y div vel y t div set div pos x div pos y var interval 0 var count 0 function iterate count div integrate 5 5 console log div pos if count 500 clearInterval interval if div pos y window innerHeight 50 div vel y 1 if div pos y 0 div vel y 1 if div pos x window innerWidth 50 div vel x 1 if div pos x 0 div vel x 1 interval setInterval iterate 100 console log window innerHeight window innerHeight console log window innerWidth window innerWidth console log done
var interval 0 var count 0 function iterate count div integrate 5 5 console log div pos if count 500 clearInterval interval if div pos y window innerHeight 50 div vel y 1 if div pos y 0 div vel y 1 if div pos x window innerWidth 50 div vel x 1 if div pos x 0 div vel x 1 interval setInterval iterate 100 console log window innerHeight window innerHeight console log window innerWidth window innerWidth console log done