How to keep the sprites updating while running a really slow function

So I'm developing the SRPG Engine plugin and one of the functions requires quite a long time to run(about 500ms). While running that function, all the sprites will stop updating, and the screen freeze. The function is about AI moving and targeting, which includes complex route searching, tile evaluation, and target evaluation, so it can be very slow when a battler has a lot of large range AoE skills and high mobility.
To simplify the situation, say you have a function like this, and it's triggered by an event:
JavaScript:
var sample = function(){
    for (var i = 0; i< 20; i++){
        for (var j = 0; j < 100000; j ++){
            var dosomething = 'dosomething' //dosomething...
        }
        //I want to put some code here to update sprites.
    }
}
After you trigger this event, the game will freeze for about 0.5s.
Is there a way to update sprites after each inner loop?
I already tried SceneManager.updateScene(), and some other methods to update the sprites, they won't work.
I feel like updating the frame is controlled by the window object, but I have no idea how to force it to update the frame.
Thank you for you help in advance.
no idea what you are trying to do, but as far i know, the function that is used to updated char sprites is:

Sprite_Character.prototype.updateCharacterFrame = function() {

it can be found in the default "rpg-sprites" plugin, its also used in a few srpg plugins:
in the srpgCore this function is used for the "E,X,A", and its used for the MapStateOverlay which i added with my "SRPG_mapStateOverlay-Plugin"..
I also made a "non srpg plugin" to add loaded imgs to the $gamePlayer, which uses this function..
And its used on the "unitMapInfo"-plugin, which shows the hp bar on units ect..

I am pretty sure by default this function also updates the event char Sprite imgs ect..
-----
If i somehow can understand better what you want to do and why that stops the sprites update, i might get better ideas, but at the moment thats all ,..sry.
no idea what you are trying to do, but as far i know, the function that is used to updated char sprites is:

Sprite_Character.prototype.updateCharacterFrame = function() {

it can be found in the default "rpg-sprites" plugin, its also used in a few srpg plugins:
in the srpgCore this function is used for the "E,X,A", and its used for the MapStateOverlay which i added with my "SRPG_mapStateOverlay-Plugin"..
I also made a "non srpg plugin" to add loaded imgs to the $gamePlayer, which uses this function..
And its used on the "unitMapInfo"-plugin, which shows the hp bar on units ect..

I am pretty sure by default this function also updates the event char Sprite imgs ect..
-----
If i somehow can understand better what you want to do and why that stops the sprites update, i might get better ideas, but at the moment thats all ,..sry.
Just try my sample code in an event and you will understand what will happen. Your code won't work.

--------‐------------------------------
After doing some research on stackoverflow I now believe the solution is to do the inner loop each time the scene map updates, or to use the set time out function. However either way I need to make sure everything else can work as normal while waiting for computing, which seems hard and can break plugin compatibility, but still doable...

Original thread: https://forums.rpgmakerweb.com/threads/how-to-keep-the-sprites-updating-while-running-a-really-slow-function.143975/