Hi, I'm using the TextEdit node and tried to trigger the compiler to compile my custom file after a second while _on_text_changed signal stop emit. Here is my code, I found it prints out many times of "time out" instead of one. I feel not confident about it, could someone give me some advice?

var _timer: Timer = null
var _on_counting : bool 

func _on_text_changed():
    if !_on_counting:
        _timer = Timer.new()
        _timer.wait_time = 2
        _timer.one_shot = true
        self.add_child(_timer)
        _timer.start()
        _on_counting = true
    elif _on_counting:
        _timer.stop()
        _timer.start(2)
        
    yield(_timer, "timeout")
    _timer.queue_free()
    _on_counting = false
    print("time out.")
    #compiler start working...

Create the Timer as a node in the tree (or rather create it with code on ready() and never delete it). Set one shot to true and set the timeout to 1 second (either in the editor or on ready). When you call start() on a Timer it resets the time, so you can easily call it as many times as you want (on every text change) and it will reset to one second each time and never fire until the user stops typing. Also, don't use yield() as this can create bugs. Simply set the timeout signal on the timer to a function, like _on_timer_timeout().

func _on_text_changed():
   _timer.start()

func _on_timer_timeout():
   print("time out.")
   #compiler start working...

@cybereality said: Create the Timer as a node in the tree (or rather create it with code on ready() and never delete it). Set one shot to true and set the timeout to 1 second (either in the editor or on ready). When you call start() on a Timer it resets the time, so you can easily call it as many times as you want (on every text change) and it will reset to one second each time and never fire until the user stops typing. Also, don't use yield() as this can create bugs. Simply set the timeout signal on the timer to a function, like _on_timer_timeout().

func _on_text_changed():
   _timer.start()

func _on_timer_timeout():
   print("time out.")
   #compiler start working...

Thanks, you saved me so many times... I know how to use a timer in a better way now.

a year later