简体   繁体   English

setTimeout 不只运行一次

[英]setTimeout is not running only once

I want to hide signup popup and display login popup by clicking 'login' link on signup modal.我想通过单击注册模式上的“登录”链接来隐藏注册弹出窗口并显示登录弹出窗口。 Which is working fine BUT problem is that setTimeout is keep opening login popup after each 1 second whereas I want to have 1 second pause and simply execute it only once.哪个工作正常,但问题是 setTimeout 每 1 秒后保持打开登录弹出窗口,而我想暂停 1 秒并只执行一次。 I've used clearTimeout(), it doesn't open login popup even one time.我使用了 clearTimeout(),它甚至一次都不会打开登录弹出窗口。 Please guide me.请指导我。 Thanks in advance!提前致谢! This is my script:这是我的脚本:

var Tid;
jQuery(document).ready(function ($) {

    //trigger login link on signup popup
    jQuery("#register-form #login-modal a").attr("href",
        "javascript:void(0);");

    jQuery("#register-form #login-modal").click(function () {
        jQuery("#register-modal-wrap").find(".mfp-close").trigger("click");
        openLoginModal();
    });

    function openLoginModal() {
        Tid = setTimeout("jQuery('#login-modal a').trigger('click')", 1000);
        //clearTimeout(Tid);
    }
});

As @Thilo has pointed out that there is a recursive triggering of click event in your code which is causing the openLoginModal() function to be called, again and again, the only way to stop that is by making sure that openLoginModal() function is called only once.正如@Thilo 指出的那样,您的代码中存在递归触发 click 事件,这导致 openLoginModal() 函数一次又一次地被调用,停止这种情况的唯一方法是确保 openLoginModal() 函数是只调用一次。 This should fix the issue.这应该可以解决问题。

var Tid;
jQuery(document).ready(function ($) {
    //trigger login link on signup popup
    jQuery("#register-form #login-modal a").attr("href",
        "javascript:void(0);");
    jQuery("#register-form #login-modal").click(function () {
        jQuery("#register-modal-wrap").find(".mfp- close").trigger("click");
        if (typeof Tid === 'undefined') {
            Tid = setTimeout(openLoginModal, 1000);
        }
    });

    function openLoginModal() {
        clearTimeout(Tid);
        jQuery('#login-modal a').trigger('click');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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