clearTimeout And setTimeout and setInterval (lag and Bug)

I why i have array incrementation when i use clearTimeout


Example whit this code


AvatarFrame[1] = 0; // the TimeOut store in this array

function Avatar1(WhatToDo) {
clearTimeout(timerAvatar[1]); // clear the timeOut Array
if (WhatToDo == 'Stop') { return;} // If call Avatar1('Stop'), this stop the function
timerAvatar[1] = setTimeout(function() { // give timeout to the array timerAvatar[1]
console.log(timerAvatar[1]); // this show the timeOut ID valur ?? i think
console.log('Avatar 1 framme'); // show TXT for fun in the SetTimeout
Avatar1(); }, 3); // CallBack Function and End of TimeOut
}


So when i call 


Avatar1();


Or


AvatarAnim(1,0,'Talk'); // in other function


This give my animation


View attachment 52434


And i notice a reduction in fps and the console.log incremented array ???.


clearTimeout.gif
I go from 120 to 109 FPS. With 


setInterval



Do you have sugestions to help me to optimise this.
That TimeOut Function i use is badly written or mismanaged by RPGmaker.
If I use the Parallels Event to display the animation directly.
I do not notice any drop in FPS


Thank you for your clarification.
This is an example for testing.
Here is the complete plugin.

Spoiler
/*:
*
*@plugindesc Gestion des menue
*
*
@author Djmisterjon
*§
*@help
*Lorsque click sur menue mais aussi le menue des dées
*
*
*/
// trick
//SceneManager.preferableRendererType = function() { return 'canvas'; }; Change perfec blend mode ( affiche les bon mode blend)
//=============================================================================
// Game_BOOT
//Booting all variable function and array.
//=============================================================================
console.log('awfdaf');
//=============================================================================
// Game Variable
//=============================================================================
AvatarFrame = [];
AvatarFrame[1] = 0; // frame toujours en evolution pour avatar 0 a 12
AvatarFrame[2] = 0; // frame toujours en evolution pour avatar 0 a 12
timerAvatar = [];
AvatarInterval = [];
oldAvatarID = [];
oldAvatarID[1] = 8; // default picture id avatar 1
oldAvatarID[2] = 9; // default picture id avatar 2
AvatarDefXy = [];
AvatarDefXy[1] = [100,650];
AvatarDefXy[2] = [220,765];

//=============================================================================
// Game arrays & objets
//=============================================================================

//=============================================================================
// Game avatar Function
//=============================================================================
//


function AvatarAnim(Avatar, ID, WhatToDo, Ax, Ay, TalkingType) {
clearInterval(AvatarInterval[Avatar]);
AvatarInterval[Avatar] = 0;
clearTimeout(timerAvatar[Avatar]);
timerAvatar[Avatar] = 0;
//Si dans le call function utilise 0 affiche au meme ID que avant ex: AvatarAnim(1,0,'Talk');
if (ID == 0) {
ID = oldAvatarID[Avatar];
}
// Si acient id nest pas egal ces que on a changer le ID Donc supprimer ancient ID
else if (oldAvatarID[Avatar] !== ID) {
var OldX = $gameScreen.picture(oldAvatarID[Avatar])._x; // utile permet de faire un MoveTo en changean de ID
var OldY = $gameScreen.picture(oldAvatarID[Avatar])._y;
$gameScreen.erasePicture(oldAvatarID[Avatar]);
$gameScreen.showPicture(ID, 'H' + Avatar + '-IDLE-' + AvatarFrame[Avatar], 1, OldX, OldY, 100, 100, 255, 0);
}
oldAvatarID[Avatar] = ID;


if (WhatToDo == 'SHOW') {
$gameScreen.showPicture(ID, 'H' + Avatar + '-IDLE-talking-happy-6', 1, Ax - 280, Ay, 100, 100, 255, 0);
$gameScreen.picture(ID)._angle = -50;
new IAVRA.ANIMATE.Tween($gameScreen.picture(ID), { _x: Ax,_angle: 0 }).easing(IAVRA.EASING.back.out).duration(25).start();
$gameScreen.setPictureCallCommon(ID, 2, 1); // lorsque click sur image call BAG
timerAvatar[Avatar] = setTimeout(function() {
$gameScreen.showPicture(ID, 'H' + Avatar + '-IDLE-0', 1, $gameScreen.picture(ID)._x, $gameScreen.picture(ID)._y, 100, 100, 255, 0);
AvatarAnim(Avatar, ID, 'ANIM', Ax, Ay);
}, 420);
} else if (WhatToDo == 'ANIM') {
AvatarInterval[Avatar] = setInterval(function() {
if (AvatarFrame[Avatar] > 12) {
AvatarFrame[Avatar] = 0;
}
$gameScreen.showPicture(ID, 'H' + Avatar + '-IDLE-' + AvatarFrame[Avatar], 1, $gameScreen.picture(ID)._x, $gameScreen.picture(ID)._y, 100, 100, 255, 0);
AvatarFrame[Avatar]++;
}, 58);
} else if (WhatToDo == 'Talk') {
// liste des type expresions parlotes
if (TalkingType == 0 || TalkingType == null) {
TalkingType = 'happy-';
}
// jouer expression
AvatarInterval[Avatar] = setInterval(function() {
if (AvatarFrame[Avatar] > 12) {
AvatarFrame[Avatar] = 0;
}
$gameScreen.showPicture(ID, 'H' + Avatar + '-IDLE-talking-' + TalkingType + AvatarFrame[Avatar], 1, $gameScreen.picture(ID)._x, $gameScreen.picture(ID)._y, 100, 100, 255, 0);
AvatarFrame[Avatar]++;
console.log('Avatar1Interval==== ' + AvatarInterval[Avatar]);
}, 58);
timerAvatar[Avatar] = setTimeout(function() {
AvatarAnim(Avatar, ID, 'ANIM', Ax, Ay);
}, 2350);

}
else if (WhatToDo == 'MoveTo') {
if (Ax == null) {Ax = $gameScreen.picture(ID)._x;} // si rien entré ces que on bouge pas X
if (Ay == null) {Ay = $gameScreen.picture(ID)._y;}// si rien entré ces que on bouge pas Y
if (Ay == null) {Ay = $gameScreen.picture(ID)._y;}// si rien entré ces que on bouge pas Y
new IAVRA.ANIMATE.Tween($gameScreen.picture(ID), { _x: Ax,_y: Ay ,_scaleX:TalkingType }).easing(IAVRA.EASING.quart.out).duration(50).start();
timerAvatar[Avatar] = setTimeout(function() {
AvatarAnim(Avatar, ID, 'ANIM', Ax, Ay);
}, 800);
}
else if (WhatToDo == 'CallBag') {
new IAVRA.ANIMATE.Tween($gameScreen.picture(ID), { _x: $gameScreen.picture(ID)._x+120,_scaleX: -100 }).easing(IAVRA.EASING.back.out).duration(25).start();
new IAVRA.ANIMATE.Tween($gameScreen.picture(ID+1), { _x: $gameScreen.picture(ID+1)._x+185,_scaleX: -100 }).easing(IAVRA.EASING.back.out).duration(25).start();
SliderOption('SHOW');
}
}


//=============================================================================
// Game_Interpreter
//=============================================================================





Tank you
The human eye can't really notice much difference above 60 FPS, so you won't be able to really see the difference except for the FPS counter that tells you how fast it's going.


SetTimeout is a property of JavaScript and not a function made by RPG Maker, so don't blame the program for its performance as they're completely unrelated. JavaScript is mostly a single-threaded programming language, meaning that unless you go out of your way to explicitly use Web Workers to run things in parallel, only one thing can happen at a time. The setTimeout function isn't really a parallel function that gets called immediately when the time limit is reached, but rather once a frame has been rendered, the JS runtime (in the case of MV, it's usually going to be Google's V8 engine) will check the list of timeouts that haven't been cleared and see if any need to be run. If they do, it runs the functions associated with them, if not it will continue on to the next frame.


I'm not sure if your comment about parallel events was a question? Were you asking if they would help, or that they made it display correctly with no drop in FPS?
JavaScript is mostly a single-threaded programming language, meaning that unless you go out of your way to explicitly use Web Workers to run things in parallel, only one thing can happen at a time.

Thank you I just learned something.


Are you talking about this?


http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_webworker
Trust me, web workers are useless. They look great in theory, but nothing works on them.
As nice as timeouts are. I wouldn't use them or setInterval unless you really have to do. If your timeout is being called in a function that updates every frame, you're only delaying the initial call. They will all stack up behind each other as if you called them regularly in an update function.


What are you trying to achieve exactly?

Original thread: https://forums.rpgmakerweb.com/threads/cleartimeout-and-settimeout-and-setinterval-lag-and-bug.71194/