繁体   English   中英

setTimeout无法正常工作

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM