timers-browserify

1 该插件封装了原生的定时器的方法

插件npm链接

2 项目中使用该插件的原因
主要是在react项目中,在自定义的组件上使用了mobox,设置了observer之后,该组件调用的setTimeout等方法,并非windows对象原生的方法,而是native function,所以import了该插件中的相关方法,从而实现原生的定时器功能
复制代码
3 相关源码
var scope = (typeof global !== "undefined" && global) ||
            (typeof self !== "undefined" && self) ||
            window;
var apply = Function.prototype.apply;

// DOM APIs, for completeness

exports.setTimeout = function() {
  return new Timeout(apply.call(setTimeout, scope, arguments), clearTimeout);
};
exports.setInterval = function() {
  return new Timeout(apply.call(setInterval, scope, arguments), clearInterval);
};
exports.clearTimeout =
exports.clearInterval = function(timeout) {
  if (timeout) {
    timeout.close();
  }
};

function Timeout(id, clearFn) {
  this._id = id;
  this._clearFn = clearFn;
}
Timeout.prototype.unref = Timeout.prototype.ref = function() {};
Timeout.prototype.close = function() {
  this._clearFn.call(scope, this._id);
};

// Does not start the time, just sets up the members needed.
exports.enroll = function(item, msecs) {
  clearTimeout(item._idleTimeoutId);
  item._idleTimeout = msecs;
};

exports.unenroll = function(item) {
  clearTimeout(item._idleTimeoutId);
  item._idleTimeout = -1;
};

exports._unrefActive = exports.active = function(item) {
  clearTimeout(item._idleTimeoutId);

  var msecs = item._idleTimeout;
  if (msecs >= 0) {
    item._idleTimeoutId = setTimeout(function onTimeout() {
      if (item._onTimeout)
        item._onTimeout();
    }, msecs);
  }
};

// setimmediate attaches itself to the global object
require("setimmediate");
// On some exotic environments, it's not clear which object `setimmediate` was
// able to install onto.  Search each possibility in the same order as the
// `setimmediate` library.
exports.setImmediate = (typeof self !== "undefined" && self.setImmediate) ||
                       (typeof global !== "undefined" && global.setImmediate) ||
                       (this && this.setImmediate);
exports.clearImmediate = (typeof self !== "undefined" && self.clearImmediate) ||
                         (typeof global !== "undefined" && global.clearImmediate) ||
                         (this && this.clearImmediate);

复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-timers 是一个基于 Vue.js 的定时器插件,它提供了一种简单的方式在 Vue 组件中管理和处理定时任务。它可以帮助开发者在 Vue 应用程序中处理各种定时操作,如定期更新数据、显示时间、执行延时操作等。 vue-timers 的主要特点是易于使用和灵活性。它提供了一组内置的指令和方法,可以轻松地管理定时器。通过使用指令,我们可以直接在模板中设置定时器,而不需要在 JavaScript 代码中编写额外的逻辑。这大大简化了定时器的使用过程,并且可以充分利用 Vue.js 的响应式能力。 另外,vue-timers 还支持循环定时器、一次性定时器以及延时执行等功能。我们可以根据需求来选择合适的方式来处理定时任务。它还提供了一些实用的方法,如取消定时器、暂停和恢复定时器等,使得我们在处理定时任务时更加灵活和可控。 使用 vue-timers,我们可以轻松地在 Vue 组件中实现各种复杂的定时操作,而不需要编写大量的代码或依赖外部库。不仅如此,vue-timers 还与 Vue 生态系统完美集成,可以充分发挥 Vue.js 的强大特性,如组件的数据绑定和响应式更新等。 总之,vue-timers 是一个功能强大且易于使用的 Vue.js 定时器插件,它为我们处理定时任务提供了便捷和灵活的方法。无论是在单个组件中还是在整个应用程序中使用,vue-timers 都能够满足我们的需求,并提供了丰富的功能和选项来处理各种定时操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值