Does python have a [js] setInterval() equivalent?

twitter logo github logo ・1 min read

I was wondering if Python (3.8+) had some kind of equivalent to JavaScript, where I can do:

var gameloop = setInterval(game_body,time)

It can be stand alone, or variable independent, though
being able to assign said equivalent to a variable
would be useful for what I am doing.
Thanks so much!

twitter logo DISCUSS (8)
markdown guide

I don't think there is anything equivalent but you could easily build a threading function that would do something similar

import threading

def set_interval(func, sec):
    def func_wrapper():
        set_interval(func, sec)
    t = threading.Timer(sec, func_wrapper)
    return t

Source: stackoverflow.com/questions/269703...


Seems like your doing game development or related work.
You can check out PyGame, which is designed for game development with Python.

I've tried pygame, and it really doesn't work for me, every time I reinstall and import it, it says it doesn't exist... also the TKinter canvas is easier for me (since it's similar to JS' canvas).
But thanks for the tip!

that's weird. which version of python you are using?

Currently: 3.9, I have tried on 3.8 and 3.7 (originally trying it on 3.8).

It says the module doesn't exist, I've tried setting my PYTHONPATH and all that fun stuff... nothing really fixed it after I updated to 3.8...
I even got the latest ver of PyGame, still no luck

Classic DEV Post from Jul 17 '19

My Remote Office Tour 🚀

Hey friends, Those of you who've been following my recent activity on Twitter already know that I am...

PDS OWNER CALIN (Calin Baenen) profile image
I am a 13 (as of Oct 30 of 2019) yr/o developer (I have been developing mini-projects for 4, years now, since I was 9), who makes projects in languages like: Java, HTML, Python 3, JS, CSS, and C#.

Signed up yet?

DEV is one of the most important resources in software.

Get Started