About using - Application Timers (bpy.app.timers)

Hello, i’m trying to use this timer in my addon, and i found problem i can’t unregister it after run.

When i’m trying use bpy.app.timers.unregistered(every_2_seconds) i getting this error: ValueError: Error: function is not registered

What do i wrong, is there way to stop timer after run?

import bpy

def every_2_seconds():
    print("Hello World")
    return 2.0

bpy.app.timers.register(every_2_seconds)

I’ve found this way to solve my problem, but still wat to hear about blender Timer.

import threading

# "constant" that we can toggle if we want to run or not.
CONTINUE = 0

def hello():
    global CONTINUE

    print("hello, world")

    if CONTINUE:
        # Create a timer object that will call the function hello after 10 seconds.
        t = threading.Timer(2.0, hello)

        # Start the timer.
        t.start()
                
hello()

I would not use the threading module with bpy, you might get some strange results. To stop a blender timer from firing, just return None when you’re done with it, as described in the documentation

I was trying all options, unfortunately it’s doesn’t work for me. I found way which work for me, when i use python threading class

The following snippet works just fine here:

import bpy

def every_two():
    print("V 2s")
    return 2.0

def unreg_in_15():
    if bpy.app.timers.is_registered(every_two):
        print("unregging")
        bpy.app.timers.unregister(every_two)
    else:
        print("no need to unreg")
    return None

bpy.app.timers.register(every_two, first_interval=2.0)
bpy.app.timers.register(unreg_in_15, first_interval=15.0)