Add counter snippet
This commit is contained in:
27
snippets/counter.md
Normal file
27
snippets/counter.md
Normal file
@ -0,0 +1,27 @@
|
||||
### counter
|
||||
|
||||
Creates a counter with the specified range, step and duration for the specified selector.
|
||||
|
||||
Check if `step` has the proper sign and change it accordingly.
|
||||
Use `setInterval()` in combination with `Math.abs()` and `Math.floor()` to calculate the time between each new text draw.
|
||||
Use `document.querySelector().innerHTML` to update the value of the selected element.
|
||||
Omit the fourth parameter, `step`, to use a default step of `1`.
|
||||
Omit the fifth parameter, `duration`, to use a default duration of `2000`ms.
|
||||
|
||||
```js
|
||||
const counter = (selector, start, end, step = 1, duration = 2000) => {
|
||||
let current = start,
|
||||
_step = (end - start) * step < 0 ? -step : step,
|
||||
timer = setInterval(() => {
|
||||
current += _step;
|
||||
document.querySelector(selector).innerHTML = current;
|
||||
if (current >= end) document.querySelector(selector).innerHTML = end;
|
||||
if (current >= end) clearInterval(timer);
|
||||
}, Math.abs(Math.floor(duration / (end - start))));
|
||||
return timer;
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
counter('#my-id', 1, 1000, 5, 2000); // Creates a 2-second timer for the element with id="my-id"
|
||||
```
|
||||
@ -34,6 +34,7 @@ composeRight:function
|
||||
converge:function
|
||||
copyToClipboard:browser,string,advanced
|
||||
countBy:array,object
|
||||
counter:browser,advanced
|
||||
countOccurrences:array
|
||||
createElement:browser,utility
|
||||
createEventHub:browser,event,advanced
|
||||
|
||||
12
test/counter/counter.js
Normal file
12
test/counter/counter.js
Normal file
@ -0,0 +1,12 @@
|
||||
const counter = (selector, start, end, step = 1, duration = 2000) => {
|
||||
let current = start,
|
||||
_step = (end - start) * step < 0 ? -step : step,
|
||||
timer = setInterval(() => {
|
||||
current += _step;
|
||||
document.querySelector(selector).innerHTML = current;
|
||||
if (current >= end) document.querySelector(selector).innerHTML = end;
|
||||
if (current >= end) clearInterval(timer);
|
||||
}, Math.abs(Math.floor(duration / (end - start))));
|
||||
return timer;
|
||||
};
|
||||
module.exports = counter;
|
||||
13
test/counter/counter.test.js
Normal file
13
test/counter/counter.test.js
Normal file
@ -0,0 +1,13 @@
|
||||
const test = require('tape');
|
||||
const counter = require('./counter.js');
|
||||
|
||||
test('Testing counter', (t) => {
|
||||
//For more information on all the methods supported by tape
|
||||
//Please go to https://github.com/substack/tape
|
||||
t.true(typeof counter === 'function', 'counter is a Function');
|
||||
//t.deepEqual(counter(args..), 'Expected');
|
||||
//t.equal(counter(args..), 'Expected');
|
||||
//t.false(counter(args..), 'Expected');
|
||||
//t.throws(counter(args..), 'Expected');
|
||||
t.end();
|
||||
});
|
||||
2150
test/testlog
2150
test/testlog
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user