[英]setTimeout not working properly
我有一个脚本,在该脚本中,单击按钮后会禁用按钮约5秒钟,然后再次启用它。
$(document).on('click', 'button', function () {
var htmls = $(this).html();
$(this).prop("disabled", true);
setTimeout(function () {
$(this).prop("disabled", false);
$(this).html(htmls);
}, 5000);
$(this).html('<img src="<?=CDN(' / icons / loading / loading5.gif ')?>" />');
});
setTimeout
不会以某种方式结束,因此不会再次启用该按钮。 我没有收到任何错误消息。
在setTimeout
调用之前将$(this)
保存到变量中,因为setTimeout
处理程序中的this
关键字引用window
对象:
$(document).on("click", "button", function() {
var $this = $(this),
htmls = $this.html();
$this.prop("disabled", true)
.html('<img src="..." />');
setTimeout(function() {
$this.prop("disabled", false)
.html(htmls);
}, 5000);
});
这里 并不是指的DOM element.Try投入另一个temarory variable.Because是外面
setTimeOut
var $this = $(this),
htmls = $this.html();
$this.prop("disabled", true);
setTimeout(function() {
$this.prop("disabled", false).html(htmls);
}, 5000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.