24be5d29cdd477ff0f3774b1ee1506c880cecba97a9a491f31a092a21afc3da2956683a82947bac35e3c0c1bac8d20da162a326c576421c2f16ca5753e0dd8 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # reInterval
  2. ![TRAVIS](https://travis-ci.org/4rzael/reInterval.svg)
  3. [![NPM](https://nodei.co/npm/reinterval.png?downloads=true&downloadRank=true)](https://nodei.co/npm/reinterval/)
  4. Reschedulable setInterval for node.js.
  5. ###### Note: Work highly inspired by [mcollina](https://github.com/mcollina)'s [retimer](https://github.com/mcollina/retimer).
  6. ## Example
  7. ```js
  8. var reInterval = require('reInterval');
  9. var inter = reInterval(function () {
  10. console.log('this should be called after 13s');
  11. }, 10 * 1000);
  12. // This will reset/reschedule the interval after 3 seconds, therefore
  13. // the interval callback won't be called for at least 13 seconds.
  14. setTimeout(function () {
  15. inter.reschedule(10 * 1000);
  16. }, 3 * 1000);
  17. ```
  18. ## API:
  19. ###`reInterval(callback, interval[, param1, param2, ...])`
  20. This is exactly like setInterval.
  21. _Arguments:_
  22. - `callback`: The callback to be executed repeatedly.
  23. - `interval`: The number of milliseconds (thousandths of a second) that the `reInterval()` function should wait before each call to `callback`.
  24. - `param1, param2, ...`: *(OPTIONAL)* These arguments are passed to the `callback` function.
  25. ####returns an `interval` object with the following methods:
  26. ###`interval.reschedule([interval])`
  27. This function resets the `interval` and restarts it now.
  28. _Arguments:_
  29. - `interval`: *(OPTIONAL)* This argument can be used to change the amount of milliseconds to wait before each call to the `callback` passed to the `reInterval()` function.
  30. ###`interval.clear()`
  31. This function clears the interval. Can be used to temporarily clear the `interval`, which can be rescheduled at a later time.
  32. ###`interval.destroy()`
  33. This function clears the interval, and will also clear the `callback` and `params` passed to reInterval, so calling this essentially just makes this object ready for overwriting with a new `interval` object.
  34. #### Note:
  35. Please ensure that either the `interval.clear()` or `interval.destroy()` function is called before overwriting the `interval` object, because the internal `interval` can continue to run in the background unless cleared.
  36. ## license
  37. **MIT**