self-timer.js
Overview
The self-timer.js is a light-weight callback runner library for javascript .
Documentation
The name of self-timer.js is originated from 'self-timer', which is a device on a camera. You may run your callback at the timing when you want to execute as if self-timer .
For instance, run some function if today is weekdays ( Monday to Friday ), self-timer.js is able to write simplify .
var st = new SelfTimer(new Date());
st.on()
.Weekdays(function() {
console.log("this method run on Monday to Friday!");
});
If you using server-side template ( exp: nunjucks, EJS.. and so. ). You can easy to switch template seasonally with self-timer.js.
{% if st.in().MonthSelects([3, 4, 5]) %}
<link rel="stylesheet" type="text/css" href="spring.css">
{% elif st.in().MonthSelects([6, 7, 8]) %}
<link rel="stylesheet" type="text/css" href="summer.css">
...
{% endif %}
Another one, output 'hello world' at 9 am to 5 pm, on Monday and Friday, between November to December. 2016.
var st = new SelfTimer(new Date());
st.in(true)
.Year(2016)
.MonthsSelects([11, 12], function() {
st.on()
.Selects(['Mon', 'Fri'], function() {
st.at()
.HoursBetween(9, 17, function() {
console.log("hello world!");
});
});
});
Installing
Using npm:
npm install self-timer --save
Using yarn:
yarn add self-timer
Using CDN:
<script src="https://unpkg.com/self-timer/dist/selftimer.min.js"></script>
<script src="https://unpkg.com/self-timer/dist/selftimer-promise.min.js"></script>
<script src="https://unpkg.com/self-timer/dist/selftimer-promise-polyfill.min.js"></script>
Available Methods
.on( )
- Sunday(), Monday(), Tuesday(), Wednesday(), Thursday(), Friday() Saturday()
- Weekday(), Weekend(), Selects()
- Annual(), DatesBetween(), DatesContain()
.at( )
- Between(), Unless()
- Min(), MinBetween(), MinSelects()
- Hour(), HoursBetween(), HourSelects()
.in( )
( supported methods-chaining on selftimer.js )
- Day(), Days(), DaysBetween()
- Month(), MonthsBetween()
- Year()
.is()
- True(), False()
- Language(), Lang()
- LanguageSelects(), LangSelects()
- LanguageExcepts(), LangExcepts()
- Mobile
.timer()
.check() (* only available for callback )
Basic Usage
self-timer.js have 2 types and 3 diffrents of files. You may choose them in right scene.
- callback style [ selftimer.js ( .min.js ) ]
- promise style [ selftimer-promise.js ( .min.js ) ]
- promise style with Polyfill [ selftimer-promise-polyfill.js ( .min.js ) ]
Note: selftimer-promise-polyfill is including taylorhakes/promise-polyfill. very thanks!
Callback
Web-Browser
<script src="./self-timer/dist/selftimer.min.js"></script>
<script>
var st = new SelfTimer(new Date());
st.on()
.Weekend(function(){
alert("it's Weekend.");
});
</script>
ES6 style & CommonJS
import SelfTimer from 'self-timer';
const st = new SelfTimer(new Date());
st.on()
.Sunday(() => {
console.log('run on Sunday!');
});
Promise
Web-Browser
<script src="./self-timer/dist/selftimer-promise-polyfill.min.js"></script>
<script>
var st = new SelfTimer(new Date());
st.on()
.Weekend()
.then(function() {
alert("it's Weekend.");
});
st.on(true)
.Weekend()
.then(function() {
alert("it's weekend.")
})
.catch(function(){
alert("it's not weekend!")
});
</script>
ES6 && CommonJS
import SelfTimer from 'self-timer/dist/selftimer-promise-polyfill.min';
const st = new SelfTimer(new Date());
st.on()
.Sunday()
.then(() => {
console.log('run on Sunday!');
});
st.on(true)
.Sunday()
.then(() => {
console.log("run on Sunday!");
})
.catch(() => {
console.log("run on if not Sunday!");
})
License
MIT