Skip to content

面向next编程,简化setTimeout, requestAnimationFrame等的调用。

Notifications You must be signed in to change notification settings

xiangwenhu/nextGenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nextGenerator

面向next编程思想,封装下一次的调用逻辑,比较经典的场景就是setTimeout,requestAnimationFrame

调用next即进入下一个周期。

代码示例

setTimeout

下面代码: 每秒调用一次回调函数, 5次后,取消调用。

const nextFactory = createTimeoutGenerator();

let context = {
    val: 0
};

let count = 0;

nextFactory.start(function (this: any, next, ...args: any[]) {

    count++;

    console.log("time:", Date.now());
    console.log("this:", this);
    console.log("args:", ...args);
    console.log(" ");
    context.val = count;

    if(count === 5){
       nextFactory.cancel();
    }

    if (count < 10) {
        // next(context, "param1-" + count, "param2-" + count);
        next(context, "param1-" + count, "param2-" + count);
    }

}, context, "param1", "param2")

About

面向next编程,简化setTimeout, requestAnimationFrame等的调用。

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published