How to create Download Button with countdown timer using HTML, CSS & JavaScript

Hello friends, today we will be going to create Download Button with countdown timer Using Html, CSS and JavaScript. I have posted many videos and articles before related to the JavaScript project, now this the something new that we are going to build.

Video Tutorial of How to create Download Button with countdown timer Using Html, CSS and JavaScript


As you have seen on the given video tutorial of  Download Button Using Html, CSS and JavaScrip, 

If you are feeling bored watching the given video tutorial of Download Button with countdown timer Using Html, CSS and JavaScript then you can copy or download the given codes below:


Download Button Using Html, CSS and JavaScript | Free Source Code

To create this program (Download Button). First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste the following codes in your file.

In the first place, make a HTML document with the name of index.html and glue the given codes in your HTML record. Keep in mind, you've to make a document with .html extension.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  <link rel="stylesheet" href="style.css" />
  <title>Download Button with timer using HTML, CSS & JavaScript</title>
  <!-- www.youtube.com/STech04 -->
</head>
<body>
  <div class="btn-container">
    <a class="download-btn" data-href="1VgJUqLM8TJY_VKK0XGKKvGpXcRlMhGe3">
      <i class="fa-solid fa-cloud-arrow-down"></i> Download Now
    </a>
    <div class="countdown"></div>
    <div class="pleaseWait-text">Please Wait...</div>
    <div class="manualDownload-text">
      Direct Link <a class="manualDownload-link">Click Here</a>
    </div>
  </div>
  <script src="./script.js"></script>
</body>
</html>
Second, make a CSS record with the name of style.css and glue the given codes in your CSS document. Keep in mind, you've to make a record with .css extension.
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: sans-serif;
}
body{
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: rgb(224, 224, 224);
}
.download-btn{
  position: relative;
  font-size: 1.3em;
  font-weight: 400;
  background: #4285f4;
  color: #fff;
  padding: 18px;
  border-radius: 30px;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.3s ease;
  box-shadow: 0 5px 25px rgba(1, 1, 1, 0.26);
}
.download-btn:hover{
  background: #116cff;
}
.download-btn i{
  margin-right: 10px;
}
.countdown{
  font-size: 1.3em;
  font-weight: 700;
}
.countdown span{
  font-size: 1.3em;
  font-weight: 800;
  color: #2e7cfa;
}
.pleaseWait-text{
  font-size: 1.3em;
  font-weight: 700;
  display: none;
}
.manualDownload-text{
  font-size: 1.3em;
  font-weight: 600;
  display: none;
}
.manualDownload-link{
  font-size: 1.1em;
  font-weight: 600;
  color: #2e7cfa;
}
Last, create a JavaScript file with the name of script.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension.
const downloadBtns = document.querySelectorAll('.btn-container');
downloadBtns.forEach((btn)=>{
    const downloadBtn = btn.querySelector('.download-btn');
    const countdown = btn.querySelector('.countdown');
    const pleaseWait_text = btn.querySelector('.pleaseWait-text');
    const manualDownload_text = btn.querySelector('.manualDownload-text');
    const manualDownload_link = btn.querySelector('.manualDownload-link');
    const download_link = downloadBtn.getAttribute("data-href");
    let timeleft = 12;
    downloadBtn.addEventListener("click",()=>{
        downloadBtn.style.display="none";
        countdown.innerHTML = "Your download will start in <span>"+ timeleft +"</span> Seconds";
        let timeInterval = setInterval(()=>{
            timeleft -=1;
            countdown.innerHTML = "Your download will start in <span>"+ timeleft +"</span> Seconds";
            if (timeleft <= 0) {
                clearInterval(timeInterval);
                countdown.style.display = "none";
                pleaseWait_text.style.display = "block";
                // here i am using google drive file download link
                let url = "https://drive.google.com/uc?export=download&id=";
                let download_href = url + download_link; // enter download file link here
                window.open(download_href);
                manualDownload_link.href = download_href;
                setTimeout(() => {
                    countdown.style.display = "none";
                    pleaseWait_text.style.display = "none";
                    manualDownload_text.style.display = "block";
                }, 1000);
            }
        },1000)
    })
})
That’s all, now you’ve successfully created a Download Button with countdown timer Using Html, CSS and JavaScrip. If your code doesn’t work or you’ve faced any error/problem, please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.
Click on the following download button to download all source code files.


Download Now
Please Wait...
Direct Link click here.

How can I help you

Post a Comment

How can I help you

Post a Comment (0)

Previous Post Next Post