繁体   English   中英

显示特定的<div> setTimeout() 处的内容</div><div id="text_translate"><p>在下面的代码中,我正在使用setTimeout()对我的后端 node.js 应用程序进行 API 调用,该应用程序每 5 秒调用一次我的 AJAX。 在我的 AJAX 成功中,我根据应至少执行一次的特定条件显示divContent1和divContent2 。 之后,在每个setTimeout()调用中,只有divContent2应该是可见的。</p><p> <strong>索引.html</strong></p><pre> &lt;script type="text/javascript"&gt; $(document).ready(function(){ $.ajax({ url: "http://localhost:8070/api/route1", type: 'POST', dataType:'json', success: function(res) { //Some Task } }); $("#myButton").click(function(){ const route2 = function() { $.ajax({ url: "http://localhost:8070/api/route2", type: "POST", dataType: "json", data: { var1: val1 }, success: function (res) { // Various tasks if(res.flag){ $("#divContent1").hide(); $("#divContent2").show(); } else{ $("#divContent1").show(); } //Functions that handle div content data }, beforeSend: function() { $("#divContent1").hide(); $("#divContent2").hide(); }, complete: function() { setTimeout(route2,5000); }, }); }; $(function(){ route2(); }) }); }); &lt;/script&gt;</pre><p> setTimeout() 调用整个route2来处理div内容的所有显示和插入。 但是,要求仅显示第二次调用的divContent2 。</p><p> 寻找解决方案</p></div>

[英]Display a specific <div> content at setTimeout()

在下面的代码中,我正在使用setTimeout()对我的后端 node.js 应用程序进行 API 调用,该应用程序每 5 秒调用一次我的 AJAX。 在我的 AJAX 成功中,我根据应至少执行一次的特定条件显示divContent1divContent2 之后,在每个setTimeout()调用中,只有divContent2应该是可见的。

索引.html

<script type="text/javascript">
$(document).ready(function(){         
    $.ajax({
             url: "http://localhost:8070/api/route1",
             type: 'POST',
             dataType:'json',                                         
             success: function(res) {
                 //Some Task               

            }
        });   
    $("#myButton").click(function(){
        const route2 = function() {
        $.ajax({
            url: "http://localhost:8070/api/route2",
            type: "POST",
            dataType: "json",
            data: { var1: val1 },
            success: function (res) { 

            // Various tasks
            if(res.flag){
                $("#divContent1").hide();
                $("#divContent2").show();
            }
            else{
                $("#divContent1").show();
            }

            //Functions that handle div content data

            },
            beforeSend: function() {
                $("#divContent1").hide(); 
                $("#divContent2").hide();
            },
            complete: function() {
                setTimeout(route2,5000);
            },
        });
    };
    $(function(){
        route2();
    })
    });
});                
</script>

setTimeout() 调用整个route2来处理div内容的所有显示和插入。 但是,要求仅显示第二次调用的divContent2

寻找解决方案

外部变量就足够了吗? 只需在外部上下文中定义它并设置/检查它以选择行为:

// before declaring button click handler
var requestDoneAtLeastOnce = false;

// ...
// somewhere in success handler
success: function (res) {
    if (!requestDoneAtLeastOnce) {
        requestDoneAtLeastOnce = true;
        // do something that belongs only to handling the first response
    }
    else {
        // this is at least the second request, the other set of commands belongs here
    }
}

setTimeout() 调用整个 route2 function 来处理 div 内容的所有显示和插入。 但是,要求仅显示第二次调用的 divContent2 。

您正在使用setTimeout(route2,5000); route2调用 route2; complete 因此,每次complete ajax 调用( successerror )时,这将无限运行。 因此,您可以做的是创建一个计时器并在第二次执行后将其清除,如下所示:

   var ctr = 0, timer =0;
   const route2 = function() {
    $.ajax({
        ...
        success: function (res) { 
         //Write you logic based on ctr
        }
        complete: function() {
            if(ctr>0){
              clearTimeout(timer)
            }else{
              timer = setTimeout(route2,5000);
              ctr = ctr+ 1;
            }

        },
     });
   };

暂无
暂无

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

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