开始10秒倒计时当我点击下载按钮
Start 10 sec countdown When i click Download Button
我找到了一个例子来做这个函数,但我想编辑它,当我点击按钮时开始,而不是在文档加载
示例:http://jsfiddle.net/rATW7/506/
var downloadButton = document.getElementById("download");
var counter = 10;
var newElement = document.createElement("p");
newElement.innerHTML = "You can download the file in 10 seconds.";
var id;
downloadButton.parentNode.replaceChild(newElement, downloadButton);
id = setInterval(function() {
counter--;
if(counter < 0) {
newElement.parentNode.replaceChild(downloadButton, newElement);
clearInterval(id);
} else {
newElement.innerHTML = "You can download the file in " + counter.toString() + " seconds.";
}
}, 1000);
你可以在jquery中添加onclick事件,或者在纯javascript中添加onclick事件。
<button id="btn">click</button>
function startDownload(){
this.style.display = 'none';
id = setInterval(function () {
counter--;
if (counter < 0) {
newElement.parentNode.replaceChild(downloadButton, newElement);
clearInterval(id);
} else {
newElement.innerHTML = +counter.toString() + " second.";
}
}, 1000);
};
var clickbtn= document.getElementById("btn");
clickbtn.onclick=startDownload;
<<p> 小提琴演示/strong> 目前你在你的HTML
中有下载按钮,你用警告段落替换,在倒计时后你再次用下载按钮替换段落。
看起来你可以避免不必要的DOM
操作,html
中的段落,并将其替换为倒计时后的下载按钮。
<p id='message'>You can download the file in<span id='count'> 10</span> seconds.</p
<button id='start'>Click to start</button
脚本var message = document.getElementById("message");
var startBtn = document.getElementById("start");
var count = document.getElementById("count");
var timer;
var counter = 10;
var downloadLink = document.createElement("a");
downloadLink.href = "downloadFile.zip";
downloadLink.className += "button";
downloadLink.innerHTML = "Download the file…";
function startDownload() {
this.style.display = 'none';
timer = setInterval(function () {
counter--;
if (counter < 0) {
message.style.display = 'none';
startBtn.parentNode.appendChild(downloadLink);
clearInterval(timer);
} else {
count.innerHTML = " "+counter.toString();
}
}, 1000);
};
startBtn.onclick = startDownload;
演示相关文章:
- Wordpress在播放器旁边播放一个自定义的下载按钮,而不是下载
- Chocolat.JS-将标题转换为下载按钮-将span类内容移动到href中
- JSPDF 下载按钮在单击时不执行任何操作.在安卓科尔多瓦
- 单击下载按钮时,应在后端进行下载
- 如何在 JSP 的 Iframe 中查看 PDF 时控制下载按钮
- PHP 下载按钮,无需提交回页面
- Piwik 从 ASP 动态生成的下载按钮后面捕获值
- 创建图像下载按钮支持所有浏览器
- Django,如何为网站上显示的脚本制作下载按钮
- Wordpress的图像下载按钮
- 当按下下载按钮时,下载带有背景图像的整个画布
- 想要创建一个下载按钮,在悬停时淡出另一个图像,然后点击第三个图像
- 使用jquery下载按钮的超链接
- 直接下载图像点击下载按钮
- 显示/隐藏下载按钮取决于用户是否在ios上
- 使用事件跟踪使用分析的下载按钮点击
- 闪亮的应用:禁用下载按钮
- 使用下载按钮从页面下载图像
- 好的上传器,习惯的方式添加下载按钮,每个文件上传
- 如何直接下载按钮与显示图像的url