react-task-z
v1.1.2
Published
Reactive hook task, executes task after timeout.
Downloads
49
Maintainers
Readme
- react task
- support task runner with timeout.
Installation
install via npm:
npm i react-task-z
Usage
Available properties
| Property | Data Type | Default Value | Property Unit |
| :---------: | :---------------: | :------------: | :-----------: |
| callback
| callback/promise
| func | |
| delay
| number
| 0 | ms
|
| maxLoop
| number
| 0 | - |
| immediate
| boolean
| false | - |
| starting
| boolean
| false | no-used |
Guilde
/**
+ run/stop
// ver>=1.1.0
task.run(); // run immediate
task.stop();
task.runAfter() // run after delay
+ maxLoop
maxLoop = 0: infinite
maxLoop > 0: limit
+ delay: run after 1 period of time
*/
sync function
import useTask from 'react-task-z';
// const [start, setStart] = useState(false); => It is recommended to use relay screen or sync function
// with basic
const task = useTask({
// starting: start,
delay: 4000,
maxLoop: 3,
// immediate: true,
callback: (loopTime, isLast) => {
console.log(loopTime, isLast);
// isLast => it will stop automatically
// if (isLast) {
// TODO something
// }
},
});
// ver>=1.1.0
task.run(); // run immediate
task.stop();
task.runAfter() // run after delay
async function
import useTask from 'react-task-z';
const task = useTask({
delay: 4000,
maxLoop: 3,
callback: async (loopTime, isLast) => {
console.log(loopTime, isLast);
const results = await fetch('https://jsonplaceholder.typicode.com/todos/1')
const info = await results.json();
console.log("Fetch called!");
// isLast => it will stop automatically
// if (isLast) {
// TODO something
// }
},
});
// ver>=1.1.0
task.run(); // run immediate
task.stop();
task.runAfter() // run after delay
License
MIT