博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to use jQuery countdown plugin
阅读量:5104 次
发布时间:2019-06-13

本文共 5217 字,大约阅读时间需要 17 分钟。

Install 

We provide two installation methods:

Bower

 

Manual

Download the files and add a script to your html:

 

Requirements

The latest versions supports jQuery from 1.7 up to >= 2.1. For legacy support (<= 1.6) please use the version .

Introduction 

For each DOM in your selector chain, an instance of the countdown will be created with an interval, that sends signals (events) with the time remaining components has weeks, days, hours, so on so forth (for more details see  section). The countdown is wrapped within the DOM and will auto delete itself when the DOM is removed.

 

With the legacy approach you will need to handle all events in a single callback (update, finish or stop) through the event.typeproperty, if you prefer an event orieted programming style, this plugin also support the default jQuery on method to register your callbacks.

 

To start the countdown the only requirement is the finalDate, but you still need register a callback to manipulate/update the DOM.

Arguments

finalDate

The target date that you are seeking to countdown. This argument can be one of the following:

  • A native date object
  • The milliseconds from Epoch time
  • String formatted as following:
    • YYYY/MM/DD
    • MM/DD/YYYY
    • YYYY/MM/DD hh:mm:ss
    • MM/DD/YYYY hh:mm:ss

callback

A function that will handle the event triggered, despite the fact we have three event types, all of them will have the same object properties (as described bellow), where you can access the offset calculation.

 

Configuration object

The plugin accepts some configuration passing by an object has the function second argument. This way one can control the precision the plugin will have and allow customizations:

 

The configuration mode is only available with the new style of initialization.

Elapse mode

The elapse mode allows the plugin to continue counting even after reaches its finish. One can control the render within the callback using the event.elapsed property.

 

Be aware that no finish event will be dispatched at this mode!

Deferred initialization

The deferred initialization mode allows you to register the callback before the countdown actually starts, this is useful when one wants to render the proper HTML the exact time the countdown starts, (i.e. when the countdown are really short time <= 60s).

Just be aware that the developer MUST call the start method manually to begin the countdown:

 

Events 

This plugin will trigger an event whenever some state change like:

  • Update: Triggered to update the DOM
  • Finish: Triggered when finished
  • Stop: Triggered that paused

To register a callback use the following event.type:

  • update.countdown
  • finish.countdown
  • stop.countdown

Be aware that ALL events should be registered with the namespace *.countdown.

Event object 

Most of the time you will be using the event.strftime function to render the countdown, the next section goes deeper in this subject. But you can access all raw values:

 

Formatter (event.strftime) 

A simple formatter that helps keep your code more semantic and avoid repetitive tasks.

It formats the offset date according to the directives in the given format string. The directive consists of a percent (%) character. Any text not listed as a directive will be passed through to the output string.

 

All the directives contains zero-padded (01, 02, 03, …, 10) and blank-padded (1, 2, 3, …, 10) versions, to use the latter please use the dash - modifier.

The formatter is also capable of handle pluralization through the bang ! modifier, by default always return the s character, if you need a complex logic please read the Pluralization topic of this section.

Modifier Description
- Blank padding
! Pluralization plugin

Directives

Directive Blank-padded Description
%Y %-Y Years left *
%m %-m Months left *
%n %-n Days left until the next complete month *
%w %-w Weeks left
%W %-W Weeks left until the next complete month *
%d %-d Days left (taking away weeks)
%H %-H Hours left
%M %-M Minutes left
%S %-S Seconds left

* Due to their non linear constrains the calculation does not have precision and it’s pretending to be used as a approximation or not use at all.

There are another set of directives that returns the full count of the time component till the end of the countdown:

Directive Blank-padded Description
%D %-D Total count of days till the end
%I %-I Total count of hours thill the end
%N %-N Total count of minutes till the end
%T %-T Total count of seconds till the end

Pluralization

The support for pluralization is built in the formatter by adding the ! (bang) modifier to the directive, the default behavior is to return the s character, it’s also possible to customize the return using the suffix :...;.

The table bellow show the supported use cases of the pluralization plugin.

Directive Description
%!H Return s when the hour is different than 1
%!S:plural; Return plural when seconds if different than 1
%!d:singular,plural; Return singular when day is 1 and plural otherwise
 

Controls 

The methods pause/resume allows to control the execution flow of the countdown:

 

There also the aliases stop/start for the same functionality:

 

To set a new final date for the countdown, call the plugin initialization function with a valid date string:

 

转载于:https://www.cnblogs.com/mouseleo/p/9604006.html

你可能感兴趣的文章
Redis-1-安装
查看>>
Access denied for user ''@'localhost' to database 'mysql'
查看>>
微信公众号里面使用地图导航
查看>>
部署支持 https 的 Nginx 服务
查看>>
‘Cordova/CDVPlugin.h’ file not found
查看>>
WebAssembly是什么?
查看>>
C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件)...
查看>>
20151214--JSTL
查看>>
树状数组_一维
查看>>
【拓扑排序】【最短路】【最小生成树】Day 9.2
查看>>
substring使用
查看>>
如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:
查看>>
java.sql.Timestamp cannot be cast to java.sql.Date
查看>>
JS代码大全-2
查看>>
linux install ftp server
查看>>
C# 使用 Abot 实现 爬虫 抓取网页信息 源码下载
查看>>
嵌入式软件设计第8次实验报告
查看>>
NP难问题求解综述
查看>>
算法和数据结构(三)
查看>>
看一下你在中国属于哪个阶层?
查看>>