Simple Countdown Timer with JavaScript

Поделиться
HTML-код
  • Опубликовано: 20 янв 2020
  • 👉 Daily Coding Challenges: iCodeThis.com/?ref=ytb-js-cou...
    In this tutorial, we're going to build a simple #Countdown timer using #JavaScript. This will be added as a browser source in OBS in the next video: • How to add a Countdown...
    #31Days31Videos
    📹 Playlist: • Playlist
    ---
    Follow me on:
    📃 Website/Blog: florin-pop.com
    👉 Twitter: / florinpop1705
    👉 Linkedin: / florinpop17
    👉 Instagram: / florinpop17
    👉 Twitch: twitch.com/florinpop17
  • НаукаНаука

Комментарии • 136

  • @thisdefinitelyworks1035
    @thisdefinitelyworks1035 4 года назад +66

    time--;
    time = time < 0 ? 0 : time;
    to avoid getting negative numbers

  • @cynthiamanor6565
    @cynthiamanor6565 3 года назад +4

    Great, straightforward, and easy to follow - thanks!

  • @syrgakomuraliev
    @syrgakomuraliev 4 года назад +7

    Thank u so much, I have a deadline tomorrow and countdown is the thing I only needed

  • @PiotrDudek72
    @PiotrDudek72 3 года назад +17

    Great job! You explained it so simply... unlike all the other sources I looked at (stack overflow, etc.).

    • @i_am_a_boy5655
      @i_am_a_boy5655 2 года назад

      So true

    • @kikevanegazz325
      @kikevanegazz325 2 года назад +1

      It's been a year since you posted this comment. I bet it was when you were starting to learn to code. If you continued on the journey, now, a year later, you'd be feeling a lot differently about StackOverflow and all the other sources you had in mind. They are also great and very useful when you know what you're looking for.

  • @comendopraengordar5761
    @comendopraengordar5761 2 года назад +1

    Subtitles help a lot in understanding the simply perfect video. Thank U!

  • @zamora322
    @zamora322 2 года назад +25

    Hi, thanks for sharing. You need a validation for negative numbers.
    let time = 0.25 * 60; //minutes * 60 seconds
    let refreshIntervalId = setInterval(updateCountdown, 1000); //update every 1 second
    function updateCountdown() {
    const minutes = Math.floor(time / 60); // rounds a number DOWN to the nearest integer
    let seconds = time % 60;
    seconds = seconds < 10 ? '0' + seconds : seconds;
    const contdownEl = document.getElementById("f1");
    contdownEl.innerHTML = `${minutes}:${seconds}`;
    time--;
    if (time < 0) { //stop the setInterval whe time = 0 for avoid negative time
    clearInterval(refreshIntervalId);
    }
    }

  • @kikevanegazz325
    @kikevanegazz325 2 года назад +1

    Very cool, and simple. Nice job!

  • @JustinTheVlogger
    @JustinTheVlogger 3 года назад +1

    Thank you for this tutorial!

  • @LuizaBarros-br5kx
    @LuizaBarros-br5kx 2 года назад

    incredible lesson!

  • @shiyamaladevik6803
    @shiyamaladevik6803 3 года назад +3

    Thanks. Simple one. It's working fine.
    With small change as minutes+":"+seconds instead of '${minutes}:${seconds}'
    And time = time < 0 ? 0: time

    • @Jameson52722
      @Jameson52722 3 года назад

      Thank you so much, the code of him dont work a little crazy. But you safed me thanks bro

    • @nyunwutumbamarcel9373
      @nyunwutumbamarcel9373 2 года назад

      Thanks a lot. This solved my struggle.

  • @ahmedn76841
    @ahmedn76841 3 года назад +1

    Thanks for doing these tutorials.I just wanna know how to use these tutorials wisely becuase when i create a project from these tutorials after a while i can't do the same project that i did before.i would really appreciate if you mention a solution for this issue.🌹🌹🙏🙏🙏😃

  • @burhanali9117
    @burhanali9117 3 года назад

    To the Point. Thank you

  • @jesssilver4244
    @jesssilver4244 Год назад

    Appreciate it, keep doing what you're doing!

  • @bilallaaroussi7110
    @bilallaaroussi7110 2 года назад

    you aren't actually guaranteed that setinterval calls the function every 1000ms, so if theres some heavy processing the timer won't be accurate.

  • @marcoonlinetv7769
    @marcoonlinetv7769 4 года назад +3

    Hello, Romania ! Greetings from Serbian border with Romania !

  • @mmisterWhite
    @mmisterWhite Год назад

    i was wondering how you could make it so it doesnt show blank on refresh but just starts with the numbers immediately?

  • @TheAlibaadshah
    @TheAlibaadshah 3 года назад +1

    if refresh the page countdown timing will be continue ....?

  • @marytmurray6701
    @marytmurray6701 3 года назад

    The countdown timer in JavaScript works when active on the timer tab but when I switch to another tab while the timer is running, it starts from 59:59. What am I doing wrong?

  • @TylerPotts
    @TylerPotts 4 года назад +1

    🔥🔥🔥🔥

    • @FlorinPop
      @FlorinPop  4 года назад +2

      💦 💦 💦 💦
      Turning down your fire 😂

  • @rabaa9352
    @rabaa9352 3 года назад

    The best tutorial.. Thanks

  • @TheAlibaadshah
    @TheAlibaadshah 3 года назад

    if reload page then countdown not stop. continue running if i logged out?

  • @marcuswright6263
    @marcuswright6263 Год назад

    Thanks great tutorial

  • @user-dt2gi6ww8h
    @user-dt2gi6ww8h 3 года назад

    appriciate your work!

  • @sleshapatil1771
    @sleshapatil1771 Год назад

    whcih link you have put in style.css???

  • @infires45
    @infires45 2 года назад +1

    what's the font from google fonts??

  • @tijanisylla1348
    @tijanisylla1348 3 года назад

    very cool, but the @import url didn't work for me.
    have any tips?

  • @nina.blacko5765
    @nina.blacko5765 3 года назад

    tysm for the tutorial

  • @JosephAli93
    @JosephAli93 Год назад

    super thanks

  • @maracineee
    @maracineee 2 года назад +1

    All my respect!

  • @DiNoooS
    @DiNoooS Год назад

    Thank you so much!

  • @user-qq2id3xk8d
    @user-qq2id3xk8d 3 года назад

    Please help! Can timer restart by itself? How to do it?

  • @poojashirsat4709
    @poojashirsat4709 Год назад

    In style.css file what you have import sir

  • @meshacharinze2077
    @meshacharinze2077 3 года назад

    Thanks a lot. Your code is precise . how follow you on your tutorial

  • @GabrielSantos-gp9yw
    @GabrielSantos-gp9yw 4 года назад +1

    Awesome man

  • @ritikbaranwal6652
    @ritikbaranwal6652 3 года назад

    Good job !

  • @akashtambe11
    @akashtambe11 3 года назад

    short and simple.. thanks

  • @jeffpatterson5783
    @jeffpatterson5783 Год назад

    Nothing changes when I add the body and p text to the CSS file

  • @maintenance_informatique
    @maintenance_informatique 8 месяцев назад

    Nice tutorial

  • @malkiatsingh3057
    @malkiatsingh3057 3 года назад +1

    what about adding user input to it?

  • @kratos1918
    @kratos1918 Год назад

    thanks bro

  • @julienaz2274
    @julienaz2274 4 года назад

    Love it and usefull, thanks !

  • @mathjunction2860
    @mathjunction2860 2 года назад

    thanks

  • @adamb3422
    @adamb3422 4 года назад +2

    :D

  • @franfallatti8404
    @franfallatti8404 3 года назад

    thx

  • @gobibratulescu9809
    @gobibratulescu9809 2 года назад

    can you do one with a Custom Animated Number Counter?

  • @ARIA.A
    @ARIA.A 3 года назад +1

    Hi can someone help me my countdown won't start i don't know why :/ i need help for my project

  • @LuisReyes-zs4uk
    @LuisReyes-zs4uk 2 года назад

    I'm trying to build this but with a button that can pause/resume the countdown... any suggestions? thank you.

  • @harshitbadaya4996
    @harshitbadaya4996 3 года назад

    Hi @
    Florin Pop , how can we add pause countdown in this..please help!

  • @brianswan661
    @brianswan661 11 месяцев назад

    for some reason I can't get the innerHTML statements to work.
    Do I have something set wrong in my editor?

    • @FlorinPop
      @FlorinPop  11 месяцев назад

      What kind of error do you get in the console?

    • @vesmo1898
      @vesmo1898 2 месяца назад

      @@FlorinPopUncaught TypeError : Cannot set properties of null (setting’innerHTML’) at updateCou ntdown

  • @Morgan-yx1wq
    @Morgan-yx1wq 4 года назад +1

    which editor are you using ?

  • @sau_peaceful_life
    @sau_peaceful_life 2 года назад

    If I make new page and open so it restart the counter again... Why is that so?
    I want to set reverse counter and should open in all tabs or pages.

  • @ShahidQureshi-ee6wz
    @ShahidQureshi-ee6wz Год назад

    great video, could you please tell me which font of vscode you are using in your video . ...

    • @FlorinPop
      @FlorinPop  Год назад

      I think it’s the default

  • @muzamilzaman7463
    @muzamilzaman7463 Год назад

    What have you written in {} brackets? ?

  • @deebx8214
    @deebx8214 3 года назад

    so helpfull

  • @poojhasree5143
    @poojhasree5143 3 года назад +1

    how to go about if the starting time is hours instead of minutes

  • @marvellousadesanya8480
    @marvellousadesanya8480 2 года назад +1

    How do you stop the countdown from going negative?

    • @FlorinPop
      @FlorinPop  2 года назад

      Put an if statement and check if it’s less than 0, then set it back to 0.

  • @horacinis
    @horacinis 3 года назад

    Say hello to ma little fren! xD

  • @GeniusHawlah
    @GeniusHawlah 3 года назад

    What if I want to include hours?

  • @ivocostilla
    @ivocostilla 3 года назад +5

    How can it be done for the counter when it reaches 0, can it be reset by itself?

  • @Arjun-yj1es
    @Arjun-yj1es Год назад

    how to stop timer after 0(without going negative)?

  • @igorkomarov6607
    @igorkomarov6607 3 года назад

    It doesn't stop after 10 minutes and counts in negative digits

  • @achaab979
    @achaab979 4 года назад +1

    hi what does percentile do?? and `${minutes}:${seconds}` its like jquerry i dont understand to that? can you please explain

    • @FlorinPop
      @FlorinPop  4 года назад +2

      The backticks ` ` is template literal

    • @jonobugs
      @jonobugs 3 года назад +1

      The percentile gives the remainder, so time % 60 will give you however many seconds are left. The part that looks like jquery is just a different way to display the code without having to use +. For example, another way to write it is minutes + ":" + seconds; That will give you the same thing. It's really just a preference in this case. You can't use the single quote, though, it needs to be the backtick which is the the left of the number one usually. and looks a bit different ` (that's the backtick) and this ' is the single quote. Looks almost the same but the single quote acts differently.

    • @lukewilki6343
      @lukewilki6343 3 года назад

      @@FlorinPop I was wondering why that didn't work!

  • @viewerLang
    @viewerLang 3 года назад +1

    how about hours and minutes?

  • @loydcose2780
    @loydcose2780 2 года назад

    It's as easy as that! Mine took roughly 50 lines of code to complete :(

  • @asher05
    @asher05 10 месяцев назад

    What is i wanted to have 2 zeros? Instead of 9:2, having 09:02

  • @peculiarpeculiar2504
    @peculiarpeculiar2504 3 года назад

    Hey florin pop

  • @Myplaylist892
    @Myplaylist892 Год назад

    Neat algorithm

  • @meldinradoncic412
    @meldinradoncic412 2 года назад

    Why doesnt work on my project?instead of counting the tjme down i got printet ${minutes}:${seconds}; ,exactly like that

    • @FlorinPop
      @FlorinPop  2 года назад +1

      You might have used quotes instead of backticks?

    • @meldinradoncic412
      @meldinradoncic412 2 года назад +1

      @@FlorinPop i got it instead of innerHTML i used innerText=minutes:seconds;

  • @rymdfararen4138
    @rymdfararen4138 Год назад

    what is wrong with my code
    const startingMinutes = 10;
    let time = startingMinutes * 60;
    const countdownEl = document.getElementById('countdown');
    setInterval(updateCountdown, 1000);
    function updateCountdown() {
    const minutes = Math.floor(time / 60);
    let seconds = time % 60;
    countdownEl.innerHTLM = `${minutes}:${seconds};`;
    time--;
    }

  • @nitinjangra6622
    @nitinjangra6622 2 года назад

    this code is not work sir plz tell me what is the mistake
    const startingMinutes = 10;
    let time=startingMinutes * 60;
    let counter = document.getElementById('clock');
    setInterval(updateCountdown,1000);
    function updateCountdown(){
    const minutes = Math.floor(time/60);
    let seconds = time % 60;
    seconds =seconds

  • @ifechukwunwachigoziri3348
    @ifechukwunwachigoziri3348 3 года назад

    Pls which text editor is this?

  • @uzohvalentine9072
    @uzohvalentine9072 Год назад

    The countdown doesn't work in my browser i need help

  • @tapasgaming5575
    @tapasgaming5575 3 года назад

    it's running in negative too how to stop that?

    • @FlorinPop
      @FlorinPop  3 года назад +1

      You need to add a condition. And if statement which will check if the seconds is less then 0

  • @Mohamed_developer_
    @Mohamed_developer_ Год назад

    The output is $minutes only displayed to me

  • @Rohan-qk4wz
    @Rohan-qk4wz 2 года назад

    ayy
    where is the source code ?

  • @harshavardhanteja6727
    @harshavardhanteja6727 3 года назад

    a small m instead capital M in Math function costs me an hour...lol

  • @devmehta7406
    @devmehta7406 4 года назад

    First🎆🎆

    • @FlorinPop
      @FlorinPop  4 года назад

      💪🏻💪🏻💪🏻

  • @cedrick7502
    @cedrick7502 Год назад

    I'm new in js ispent hours thinking ` is same as ' F

  • @MuhammadKhan-vm5ow
    @MuhammadKhan-vm5ow Год назад

    it doesnt stop at zero

  • @TesterAnimal1
    @TesterAnimal1 2 года назад

    That’s terrible.
    `setInterval` is not accurate. It will drift and not be accurate.
    HTTP 203 covered the correct way to do this.

  • @smarthead7131
    @smarthead7131 Год назад

    `

  • @Mohamed_developer_
    @Mohamed_developer_ Год назад

    This is not worked 😑 what problem 😐 I don't know 😕

  • @TheMatthyssen
    @TheMatthyssen 4 года назад

    how do you do it so it shows a zero on the left when the time is under 10min?
    so:
    10:00
    09:59
    09:58
    etc
    instead of
    10:00
    9:59
    9:58

    • @FlorinPop
      @FlorinPop  4 года назад +1

      Check to see if the number is less then 10. If it is then add a 0 at the beginning

  • @froststorm77
    @froststorm77 4 года назад +1

    Your timer will go below zero

    • @FlorinPop
      @FlorinPop  4 года назад

      Good catch 👍🏻

    • @codeswithankit4316
      @codeswithankit4316 4 года назад +7

      instead of time-- use time !== 0 ? time-- : time;

    • @raphalugs
      @raphalugs 4 года назад

      @@codeswithankit4316 thanks 😊

    • @NightfallNarative
      @NightfallNarative 4 года назад +2

      function ifzero(){
      if(time === 0){
      time = strttime * 60;
      }
      }