Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
Create a Pomodoro Timer using HTML CSS and JavaScript
Next article icon

Create A Download Button with Timer in HTML CSS and JavaScript

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss how to create a Download button with a timer attached to it using HTML, CSS, and Javascript.

Our goal is to create a button that has a timer attached to it. The download should only start after the timer has run out, which we will achieve using the setTimeout and setInterval functions of JavaScript. We will use these functions to create a timer that waits for the required amount of time before initiating the download when the button is clicked.

Screenshot-2023-11-16-115014
Preview

Approach

  • Create a HTML structure with heading, button, and countdown div, assigning unique IDs for styling and scripting.
  • Apply CSS to enhance the visual appearance, adjusting properties like padding, font size, and background color.
  • Use document.getElementById or document.querySelector to select specific HTML elements for interaction.
  • Attach a click event listener to the download button to initiate the timer logic.
  • Add a timer using setInterval, updating the countdown display, and triggering the download process after a specified duration.

Note: Add your download link to the required url in the downloadLink variable in Javascript file to initiate the download. In this we have a file called sample_text_file.txt in the dowload_content folder.

Example: In this example, we will design a webpage to see the download of a file from a download button.

HTML
<!DOCTYPE html>  <html lang="en">   <head>  	<meta charset="UTF-8" />  	<meta name="viewport" content=  		"width=device-width, initial-scale=1.0" />   	<title>Download Button with Timer</title>  	 	<style>  		body {  			display: flex;  			flex-direction: column;  			align-items: center;  			justify-content: center;  			height: 100vh;  			margin: 0;  			font-family: sans-serif;  		}   		h1 {  			color: #308d46;  		}   		#downloadButton {  			padding: 10px 20px;  			font-size: 16px;  			background-color: #007bff;  			color: #fff;  			border: none;  			cursor: pointer;  		}   		#countdown {  			display: none;  			font-size: 24px;  			margin-top: 20px;  		}  	</style>  </head>   <body>  	<h1>GeeksForGeeks</h1>   	<h4>  		This is a download button  		with a timer attached  	</h4>   	<button id="downloadButton">  		Download  	</button>  	 	<div id="countdown"></div>   	<script>  		const downloadButton = document.getElementById("downloadButton");  		const countdown = document.getElementById("countdown");  		const downloadLink = "download_content/sample_text_file.txt";   		let timer;  		let countdownValue = 5;   		downloadButton.addEventListener("click", function () {  			countdown.style.display = "block";   			timer = setInterval(function () {   				if (countdownValue <= 0) {  					clearInterval(timer);  					countdown.innerHTML = "Downloading...";  					setTimeout(function () {  						const a = document.createElement("a");  						a.style.display = "none";  						a.href = downloadLink;  						a.setAttribute("download", "");  						document.body.appendChild(a);  						a.click();  					}, 1000);  				} else {  					countdown.innerHTML =  				`Starting download in ${countdownValue} seconds...`;  				}  				countdownValue--;  			}, 1000);  		});  	</script>  </body>   </html> 

Output:


Next Article
Create a Pomodoro Timer using HTML CSS and JavaScript
author
ragul21
Improve
Article Tags :
  • Web Technologies
  • Geeks Premier League
  • JavaScript-Projects
  • Geeks Premier League 2023

Similar Reads

  • Create a Button Loading Animation in HTML CSS & JavaScript
    A "Button Loading Animation" in HTML, CSS, and JavaScript is a user interface element that temporarily transforms a button into a loading state with a spinner or animation to indicate ongoing processing or data retrieval, providing feedback to users during asynchronous tasks. Approach: HTML page wit
    2 min read
  • Create a Quiz App with Timer using HTML CSS and JavaScript
    Creating a quiz app is an excellent way to learn the fundamentals of web development. In this tutorial, we will build a Quiz App that features a timer, allowing users to take a timed quiz with multiple-choice questions. The app will use HTML for the structure, CSS for styling, and JavaScript for fun
    8 min read
  • How to create waves on button with CSS and HTML?
    The wave effect on a button is a type of effect in which the shape of the button turns into a wave on hovering. While there are other ways to create a wave effect, a simple method is to use the keyframe property. Approach: In order to animate the button, we use keyframe to gradually set the transiti
    2 min read
  • How to Trigger a File Download when Clicking an HTML Button or JavaScript?
    Triggering file downloads in JavaScript refers to initiating the download of files directly from a webpage when a user clicks a button or link. This can be achieved using HTML5's download attribute or custom JavaScript functions, allowing dynamic and user-friendly file-downloading experiences. To tr
    4 min read
  • Create a Pomodoro Timer using HTML CSS and JavaScript
    In this article, we will see how to design Pomodoro Timer with the help of HTML CSS, and JavaScript. The Pomodoro Technique is a time management method that involves breaking your work into focused intervals, typically 25 minutes, followed by short breaks. After completing four cycles, take a longer
    3 min read
  • How to create Popup Box using HTML CSS and JavaScript?
    Creating a popup box with HTML, CSS, and JavaScript improves user interaction on a website. A responsive popup appears when a button is clicked, featuring an HTML structure, CSS for styling, and JavaScript functions to manage visibility. ApproachCreate the Popup structure using HTML tags, Some tags
    3 min read
  • Create a Math Sprint Game in HTML CSS & JavaScript
    The math sprint game presents random math questions with four options. Players click the correct answer, and if correct, it's acknowledged; otherwise, an incorrect answer message is shown. A 20-second timer adds urgency, ending the game. The score (High Score) is displayed, along with buttons to sta
    5 min read
  • Create a Build A Simple Alarm Clock in HTML CSS & JavaScript
    In this article, we will develop an interactive simple Alarm Clock application using HTML, CSS, and JavaScript languages. Develop an alarm application featuring date and time input fields for setting alarms. Implement comprehensive validation to prevent duplicate alarms and enforce a maximum limit o
    5 min read
  • Create a File Upload with Progress Bar in HTML CSS & JavaScript
    In a file upload with a progress bar application using HTML, CSS, and JavaScript where users can be able to upload the image file, with a live dynamic progress bar of uploading. The uploaded image can be previewed in modal. Users can also clear the uploaded image in the application. PrerequisitesHTM
    5 min read
  • Design a Simple Counter Using HTML CSS and JavaScript
    Creating a counter application with click tracking is a great beginner-friendly project to enhance your understanding of JavaScript, HTML, and CSS. In this article, we will guide you through building a basic counter app where you can increment or decrement a value, track the number of clicks, and re
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences