timmer Functions

Global Functions

In Browser Enviroment

Timer functions are used to delay or repeat the execution of other function which they receive as arguments

setTimeout

setTimeout(() => {
  console.log("Hello after 4 seconds");
}, 4 * 1000);
const func = () => {
  console.log("Hello after 4 seconds");
};

setTimeout(func, 4 * 1000);

// For: func(arg1, arg2, arg3, ...)
// We can use: setTimeout(func, delay, arg1, arg2, arg3, ...)
const rocks = (who) => {
  console.log(who + " rocks");
};

setTimeout(rocks, 2 * 1000, "Pluralsight");

Challenge

// solution 1
const theOneFunc = () => {
  console.log("Hello after 4 seconds");
  setTimeout(() => {
    console.log("Hello after 8 seconds");
  }, 8 * 1000);
};

setTimeout(theOneFunc, 4 * 1000);

// Hello after 4 seconds

// Hello after 8 seconds

// You can define only ONE function
// solution 2
const theOneFunc = (delay) => {
  console.log("Hello after " + delay + " seconds");
};

setTimeout(theOneFunc, 4 * 1000, 4);

setTimeout(theOneFunc, 8 * 1000, 8);

setInterval

setInterval execute the fucntion after the setInterval infinite time, untill you kill that function.

Previous
Next