Methodqueue: Replace window. setTimeout and reuse timers

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.