1const delay = 2;
2const limit = 2;
3let i = 1;
4
5console.log('START!');
6const limitedInterval = setInterval(() => {
7 console.log(`message ${i}, appeared after ${delay * i++} seconds`);
8
9 if (i > limit) {
10 clearInterval(limitedInterval);
11 console.log('interval cleared!');
12 }
13}, delay * 1000);
1const App = () => {
2 const [loading, setLoading] = React.useState(true);
3 React.useEffect(() => {
4 const interval = setInterval(() => {
5 setLoading(false);
6 }, 3000);
7 return () => clearInterval(interval);
8 }, []);
9 return (
10 <div className="App">{loading ? <h1>Loading</h1> : <h2>Counter</h2>}</div>
11 );
12}
13ReactDOM.render(<App />, document.getElementById("root"));