If window. setTimeout is frequently used on the page, the Code judgment may be confusing.
For example
If (! Timerid)
Timerid = Window. setTimeout (dosomething, duration );
Window. cleartimeout (timerid );
And so on.
Methodqueue is an object used to manage setTimeout. It can effectively manage and reuse timer. You only need to call methodqueue. Add (ID, delay, resettimer, function); methodqueue. Remove (ID, delay ).
The parameter meanings are as follows:
ID: ID of the timer
Delay: How long after execution
Resettimer: If this ID exists, whether to reset its delay.
Function: callback execution function.
Example:
Methodqueue. Add ('myflake. mycallback', 1000, true, function (){....});
Methodqueue's code: VaR methodqueue =
{
Delays: [],
Timerids: [],
Add: function (ID, delay, resetifexist, func)
{
VaR queue = methodqueue. Delays [delay]; // get the queue for this delay
If (null = Queue) queue = [];
If (null = queue [ID]) // first time registration
{
Queue [ID] = {ID: ID, code: func, reset: false}; // register the function in the delay queue
}
Else
{
// Same ID, same delay already exist. If reset defined, then store the new function and discard previous one
If (resetifexist) {queue [ID] = {ID: ID, code: func, reset: true };}
Else {return ;}
}
Methodqueue. Delays [delay] = queue; // register the queue in the delay
If (methodqueue. timerids [delay] = NULL)
{
Methodqueue. timerids [delay] = Window. setTimeout (function ()
{
Methodqueue.exe cute (Delay );
}, Delay );
}
},
Execute: function (Delay)
{
VaR delayqueue = methodqueue. Delays [delay];
For (var key in delayqueue)
{
VaR item = delayqueue [Key];
If (item! = NULL)
{
If (typeof item. Code = "function ")
{
If (! Item. Reset) // if this method was not reset, then execute it
{
Delayqueue [Key] = NULL; // remove this item, incase the code tries to register again, it will get this key free
Item. Code (); // execute the code, note it might register the timer callback again
Delete item. Code;
}
Else
{
Item. Reset = false;
}
}
}
}
// Find out if there's any callback left in the queue.
// It might happen that, when a callback was called, it
// Registered in the same delay queue again
VaR allcallbackcalled = true;
For (var key in delayqueue)
{
VaR item = delayqueue [Key];
If (item! = NULL)
{
If (typeof item. Code = "function ")
{
Allcallbackcalled = false;
}
}
}
If (allcallbackcalled) // if all callbacks in the queue fired, we can remove this queue and Timer
{
Delete delayqueue;
Methodqueue. Delays [delay] = NULL;
Delete methodqueue. Delays [delay];
Window. cleartimeout (methodqueue. timerids [delay]);
Methodqueue. timerids [delay] = NULL;
Delete methodqueue. timerids [delay];
}
Else
{
// Re-register the timer
Methodqueue. timerids [delay] = Window. setTimeout (function ()
{
Methodqueue.exe cute (Delay );
}, Delay );
}
},
Exist: function (ID, delay)
{
VaR queue = methodqueue. Delays [delay];
If (null = Queue) queue = [];
Return (null! = Queue [ID]);
},
Remove: function (ID, delay)
{
VaR queue = methodqueue. Delays [delay];
If (null = Queue) return;
Else queue [ID] = NULL;
Methodqueue. Delays [delay] = queue;
}
}
Original artical: methodqueue: Replace window. setTimeout and reuse timers