JavaScript Lessons

JavaScript setTimeout: Schedule Functions With Delay

Use JavaScript setTimeout to run a function once after a delay you choose. See how timers, callbacks and clearTimeout work in real examples.

Serhat ALPASLAN1 min read

Javascript setTimeout

setTimeout is a built-in function that ships with JavaScript. The setTimeout function counts in milliseconds and runs the function once the specified time has passed—so if you want to run a function after a certain delay, you can use setTimeout.

Aşağıdaki şekilde bir yazım şekli vardır 
setTimeout(fonksiyonunuz, milisaniyeSure);

Now let’s look at the example below;

function testFonksiyon(){
   console.log('setTimeout fonksiyonu çalıştı')
  }
  setTimeout(testFonksiyon, 3000); //3000 milliseconds equals 3 seconds, so for 1 second you need to write 1000.

We can also call a function that takes parameters with setTimeout.

function testFonksiyon(adsoyad, eposta){
    console.log("Ad Soyad: " + eposta);
     console.log("E-Posta Adresi: " + adsoyad);
  }
  setTimeout(testFonksiyon, 1000, "Serhat ALPASLAN", "serhat@serhatalpaslan.com");

The JavaScript setTimeout function runs only once and does not repeat. So if you want a function that repeats the same operation at the interval you specify, this time you need to use JavaScript setInterval.

You can also check out the official website.

Share it if it helped

Write a comment