pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/d0peCode/cancelableInterval

githubassets.com/assets/primer-70be7debc79a8eff.css" /> GitHub - d0peCode/cancelableInterval: Cancel asynchronous interval · GitHub
Skip to content

d0peCode/cancelableInterval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

cancelable interval

Why?

When you have interval with asynchronous code inside and you need to prevent interval from being called again before asynchronous code in previous iteration is completed you need to clearInterval by its id at the top of interval function body and then start it again and reassign id at the end of it.

Issue with this is that you will never be able to stop such mechanism even when you return id because its id will be reassign over and over again.

Solution is to have a function which will act like setInterval but additionally provide cancellation feature and actually return cancel function instead of interval id.

This is what this little project aim for.

Usage demo

const cancelableInterval = require('../dist/cancelableInterval');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

const foo = () => {
    const intervalFunction = async() => {
        console.log('interval executed');
        await sleep(120); //some long action
    };
    return cancelableInterval(intervalFunction, 100); //returning id
};

(() => {
    const cancellableIntervals = [];
    cancellableIntervals.push(foo());

    setTimeout(() => {
        console.log("Cancelling 100ms interval with 120ms of async code runtime after 700ms.");
        console.log("(100ms + 120ms) * 5 = 660ms so interval should run 3 times.");
        cancellableIntervals.forEach(cancel => { cancel() });
    }, 700)
})();

sleep function is just to simulate asynchronous code which runtime is longer than interval time gap.

In above code I have foo function which contains interval' function and execute cancelableInterval which return cancel(). Then in IIFE I'm executing foo and pushing its cancel() to cancellableIntervals array. After 700ms I'm cancelling every element of cancellableIntervals (one in example for simplicity).

About

Cancel asynchronous interval

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy