You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			26 lines
		
	
	
		
			589 B
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			26 lines
		
	
	
		
			589 B
		
	
	
	
		
			TypeScript
		
	
| 
											5 years ago
										 | import React from 'react'; | ||
|  | 
 | ||
|  | export const useInterval = (callback: any, delay: number | null) => { | ||
|  |   const savedCallback = React.useRef<any>(); | ||
|  | 
 | ||
|  |   React.useEffect(() => { | ||
|  |     savedCallback.current = callback; | ||
|  |   }, [callback]); | ||
|  | 
 | ||
|  |   React.useEffect(() => { | ||
|  |     function tick() { | ||
|  |       if (savedCallback && savedCallback.current && savedCallback.current) { | ||
|  |         savedCallback.current(); | ||
|  |       } | ||
|  |     } | ||
|  |     if (delay !== null) { | ||
|  |       const id = global.setInterval(tick, delay); | ||
|  |       tick(); | ||
|  |       return () => { | ||
|  |         global.clearInterval(id); | ||
|  |       }; | ||
|  |     } | ||
|  |     return; | ||
|  |   }, [delay]); | ||
|  | }; |