轻量级功能丰富 定时器插件 jQuery tinytimer

  • 源码大小:18.07KB
  • 所需积分:1积分
  • 源码编号:19JP-3058
  • 浏览次数:984次
  • 最后更新:2023年05月01日
  • 所属栏目:时间和时钟
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

一种轻量级的、功能丰富的jQuery插件创建计时器容易管理计时器在您的项目。

tinytimer插件允许您根据需要保存和恢复计时器实例。即使你的电脑关机了,它也能确保你的计时器保持跟踪。

此外,该插件还支持动态计数器更新、可自定义的时间格式以及基本的onTick()和onTargetReachd()事件处理程序。

如何使用它:

1.在文档中下载并加载tinytimer插件的压缩版(~2.5kb)。

  1. <script src="/path/to/cdn/jquery.min.js"></script>
  2. <script src="/path/to/dist/jquery.tinytimer.min.js"></script>

2.创建一个新的计时器实例。

  1. <div class="jt-counter">
  2. Counter!
  3. </div>
  1. const myTimer = $(".jt-counter").timer();

3.自定义计时器的可用选项。

  1. $(".jt-counter").timer({
  2.  
  3. // initial value
  4. timerCounter: 0,
  5.  
  6. // target value
  7. timerTarget: 0,
  8.  
  9. // tick interval in milliseconds
  10. refreshInterval: 1000,
  11.  
  12. // formatter function
  13. updateHtml: function() {
  14. // using getTimezoneOffset makes this work with all timezones
  15. var d = new Date( "Thu Jan 01 1970 00:00:00" );
  16. var f = new Date( this.counter + d.getTimezoneOffset() * 60000 );
  17. this.html(
  18. ( "0" + ( ( f.getDate() - 1 ) * 24 + f.getHours() ) ).substr( -2 ) + ":" +
  19. ( "0" + ( f.getMinutes() ) ).substr( -2 ) + ":" +
  20. ( "0" + ( f.getSeconds() ) ).substr( -2 )
  21. );
  22. },
  23.  
  24. });

4.事件处理程序

  1. $(".jt-counter").timer({
  2.  
  3. onTargetReached: function() {
  4. // do something here
  5. },
  6. onTick: function() {
  7. console.log(
  8. "Counter value: " + this.counter + "\n" +
  9. "Target: " + this.target + "\n" +
  10. "Is Running: " + this.enabled + "\n" +
  11. "Current history: " + JSON.stringify(this.history)
  12. );
  13. }
  14.  
  15. });

5.管理计时器的可用方法。

  1. // start
  2. myTimer.start();
  3.  
  4. // stop
  5. myTimer.stop();
  6.  
  7. // reset
  8. myTimer.zero();
  9.  
  10. // destroy
  11. myTimer.destroy();
  12.  
  13. // remove
  14. myTimer.remove();
  15.  
  16. // update
  17. myTimer.counter = 60*1000;

预览截图