Skip to main content

Command Palette

Search for a command to run...

Let's heat our coffee using setTimeout() in JS

Updated
2 min read
Let's heat our coffee using setTimeout() in JS

If you love your hot coffee, you don't wanna have it to be served cold.
And there are times when you make your cup of coffee and keep it as it is on your working desk, without having even a single sip.


So, we decide to heat it quickly, rather than drinking it at room temperature.
Okay. I know it's not a big deal to do that. But, what if your microwave asks you to set the timer using JavaScript?
Of course, it also gets bored by doing the same task frequently.
Well, Okay! Let's do it!



Now, to perform the task, our microwave just needs to write the code for the timeout of heating our coffee.
The rest of the things like setting minimum temperature and staring to heat will be done by itself, as soon as we set the timeout.
Why and how? Because our microwave is our kind friend.



Okay! Let's write the code snippet for it.

First, we add a function named HeatCoffee and log a message that our coffee will get heated in 20 seconds.


const HeatCoffee = () => { 
console.log('Your Coffee will be ready in less than 20s 👍');
};
HeatCoffee();

Then, we will add the setTImeout() inside HeatCoffee() to set the timeout for 20 seconds.


const HeatCoffee = () => { 
console.log('Your Coffee will be ready in less than 20s 👍');
    {
        setTimeout(() => {
        console.log('Hey 🙌 Your coffee is ready ☕️')
        }, 20000);
     }

};
HeatCoffee();

Hurray! We have won our hot coffee.
🙌 Enjoy!
☕️

You can read the document on Concurrency model and event loop in javascript through the link below. 👇🏼

https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

Also, don't forget to say thanks to your microwave. 😇

S

That was an amazing way of writing✍️💯.Keeping audience engaged and a fun way of learning. Great one🥳

2
A

That's pretty nice way to teach something Anna, I like the way Well done🎉

1
D

Thanks Aman.

3

Cook with code

Part 1 of 1

In this series, I'll be sharing articles related to coding while cooking.

More from this blog

Technical Artist

14 posts