在下载或者上传文件过程中避免重复点击带来的多次同样的请求造成资源浪费,限制 button 的点击次数是很有必要的。 1. 增强用户体验,2. 减轻服务器压力。
HTML 代码
JS 代码
var foo = function(element) { var intent = document.querySelector(element) intent.disabled = 'disabled' setTimeout(function() { intent.disabled = '' }, 3000) /* 可以把 3000 改成自定义的时间*/ console.log('3 秒内不可以重复点击')}var bar = function(fun, select) { var intent = document.querySelector('#test') intent.addEventListener('click', function() { fun(select) })}bar(foo, "#test")