+ 1
How to create a loop that runs every X seconds?
In Javascript && || Python
4 Answers
+ 2
import time
print("Printed immediately.")
while True:
time.sleep(2.4)
print("Printed after 2.4 seconds.")
This will run forever.
+ 2
doing things asynchroniously in python is far to obvious ^^
in javascript, that's easiest to implement, using setTimeout, setInterval, or eventually requestAnimationFrame:
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
+ 1
Jerry Hobby solution is right, but run synchroniously... it means that you cannot do anything else while delay...
+ 1
using requestAnimationFrame should be more accurate, but requires to use its timestamp argument to know if delay is expired or not (and accordingly run or not your body loop)