Giter Site home page Giter Site logo

pausable-timer's Introduction

pausable-timer

介绍

一个可随时多次暂停以及恢复执行的定时器类,可在浏览器以及node.js运行时使用

简单使用

安装

npm i pausable-timer
# or
yarn add pausable-timer
# or
pnpm i pausable-timer
  import PausableTimer from "pausable-timer";
  const pt = new PausableTimer({
    // 运行模式,只限于'loop','once'
    mode: "loop",
    // 调用周期,毫秒
    timer: 2000,
    // 回调
    callback: () => {
      console.log("panda");
    },
    // 可选,是否开启调试,控制台会打印一些额外信息
    debug: true,
  });
  // 暂停
  pt.pause();
  // 恢复
  pt.resume();
  // 暂停2秒之后恢复
  pt.after(2000);
  // 改变运行模式,['loop'|'once'],第二个参数为可选,是否重新计时
  pt.setMode("loop", true);
  // 停止运行
  pt.stop();

可基于此类封装为任意你喜欢的使用方式.example👇

  const mySetInterval = (callback, delay) => {
    return new PausableTimer({
      callback,
      timer: delay,
      mode: "loop",
    });
  };
  mySetInterval(() => {
    console.log("666");
  }, 1000);
  const myClearInterval = (t) => {
    t.stop();
  };

pausable-timer's People

Contributors

bbbboys avatar

Watchers

 avatar

pausable-timer's Issues

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.