UNPKG

@rooks/use-countdown

Version:

Count down to a target timestamp and call callbacks every second (or provided peried)

3 lines (2 loc) 939 B
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).useCountdown=t(e.React)}(this,(function(e){"use strict";return function(t,n={}){const{interval:u=1e3,onDown:r,onEnd:o}=n,[c,f]=e.useState((()=>new Date)),i=t.getTime()-c.getTime(),l=i>0?Math.ceil(i/u):0;return function(t,n,u=!1){const r=e.useRef(null),[o,c]=e.useState(u),f=e.useRef();function i(){o||c(!0)}function l(){o&&c(!1)}let s;e.useEffect((()=>{f.current=t})),e.useEffect((()=>{if(null!==n&&o){let e=setInterval((function(){f.current&&f.current()}),n);return r.current=e,()=>{r.current=null,clearInterval(e)}}}),[n,o]),s=[i,l,r.current],s.start=i,s.stop=l,s.intervalId=r.current}((function(){const e=new Date;if(e>t)return o&&o(e),void f(t);r&&r(i,e);f(e)}),l?u:null,!0),l}})); //# sourceMappingURL=index.min.js.map