JavaScript regular intervals execution time elapsed stop

Execute Script Periodically and Stop After Time Limit in JavaScript

A memo about sample code that executes at regular intervals while stopping after a certain amount of time in JavaScript. Implemented using setInterval and clearInterval. An example that logs every 0.2 seconds and stops after 10 seconds

Shou Arisaka
1 min read
Oct 16, 2025

A memo about sample code that executes at regular intervals while stopping after a certain amount of time in JavaScript.

Implemented using setInterval and clearInterval.

An example that logs every 0.2 seconds and stops after 10 seconds:

var refreshIntervalId = setInterval(function(){ console.log('y'); } , 200);

setTimeout(function(){ clearInterval(refreshIntervalId); } ,10000) ;

Share this article

Shou Arisaka Oct 16, 2025

๐Ÿ”— Copy Links