Git Product home page Git Product logo

mainloop's Introduction

mainLoop.js

主循环,顾名思义,就是建立一个与浏览器刷新频率相同的执行循环。

所有需要与浏览器刷新频率同步的操作都可以加入到这个循环中,同时也可以在需要时从循环中移除这个操作。

这个插件主要是基于 HTML5 的 requestAnimationFrame API 编写的。

Usage

安装

$ npm install mainloop

使用

var mainLoop = require('mainloop');
var count = 0;
mainLoop.add(function() {
    // loop action
    count++;
    if (count === 100) {
        return false; // 当返回 false 时,该方法会从主循环中移除
    }
});

API

如果是通过 <script> 标签引入,那么可以通过全局对象 mainLoop 进行调用。

// func 是你要循环操作的方法对象。
// target 是循环操作的上下文,即 this 的指向。
mainLoop.add(func, target); 

// func 可选,必须是 add 进循环的方法。如果是一个闭包函数,则不会有任何效果。
// 如果 func 不是一个方法,则等同于无参数。此时 remove 会移除掉之前所有 add 进循环的方法。
mainLoop.remove(func);

// 停止循环
mainLoop.stop();

// 启动循环
mainLoop.play();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.