+ 1

How to create a loop that runs every X seconds?

In Javascript && || Python

19th Mar 2021, 8:14 PM
Matheus Zotti
Matheus Zotti - avatar
4 Respuestas
+ 2
import time print("Printed immediately.") while True: time.sleep(2.4) print("Printed after 2.4 seconds.") This will run forever.
19th Mar 2021, 8:51 PM
Jerry Hobby
Jerry Hobby - avatar
+ 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
19th Mar 2021, 9:40 PM
visph
visph - avatar
+ 1
Jerry Hobby solution is right, but run synchroniously... it means that you cannot do anything else while delay...
19th Mar 2021, 9:37 PM
visph
visph - avatar
+ 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)
19th Mar 2021, 9:40 PM
visph
visph - avatar