Skip to main content

Command Palette

Search for a command to run...

[JavaScript] Numbers, Dates, Intl, and Timers - Timers: setTimeout and setInterval

Updated
•View as Markdown
[JavaScript] Numbers, Dates, Intl, and Timers - Timers: setTimeout and setInterval
// Set Timeout
const ingredients = ['olives', 'spinach'];
const pizzaTimer = setTimeout(
  (ing1, ing2) => console.log(`Here is your pizza with ${ing1} and ${ing2} 🍕`),
  3000, // 1s =1000 ms
  ...ingredients
);
console.log('Waiting... (after 3 seconds later)');

if (ingredients.includes('spinach')) clearTimeout(pizzaTimer);
  • no pizza... setTimeout 1.PNG
const ingredients = ['olives', ''];
const pizzaTimer = setTimeout(
  (ing1, ing2) => console.log(`Here is your pizza with ${ing1} and ${ing2} 🍕`),
  3000, // 1s =1000 ms
  ...ingredients
);
console.log('Waiting... (after 3 seconds later)');

if (ingredients.includes('spinach')) clearTimeout(pizzaTimer);
  • pizza with olives

setTimeout 2.PNG

// Set Interval
setInterval(function () {
  const now = new Date();
  console.log(now);
}, 1000);

setInterval 1.PNG

// clock formatting
setInterval(function () {
  const now = new Date();
  const hour = now.getHours();
  const min = now.getMinutes();
  const sec = now.getSeconds();
  console.log(`${hour}:${min}:${sec}`);
}, 1000);

setInterval 2.PNG

// applying to the app
// Add movement with delay(approvement)
 setTimeout(function () {
      currentAccount.movements.push(amount);
      console.log('2.5 seconds later'); // only for checking setTimeout worked well...
      // add loan date
      currentAccount.movementsDates.push(new Date().toISOString());

      // Update UI
      updateUI(currentAccount);
    }, 2500);

loan delay 1.PNG

loan delay 2.PNG

JavaScript

Part 1 of 50

More from this blog

Jayden's Blog

166 posts