Function only use "leftover" time?

Hello, i was wondering if there is any way to tell a function to only use “leftover” time from a frame. I have a heavy function that uses many seconds to finish, so every time i call it the game will freeze until its done. I have experimented with yielding in the function, and it kinda works, but its hard to figure out how much to yield. Too little yielding, and the frame rate suffers, too much yielding and the function takes forever to finish. So, are there any built in functionality that lets a function only use “leftover” time?

Alternatively, if anyone have a good algorithm for doing this with yielding, please share it with me :slight_smile:

yes, there’s recent a massive, expert explanation by WHydoidoit, right here