开发者

ajax jquery updating div problem

开发者 https://www.devze.com 2023-01-19 05:35 出处:网络
I am trying to use ajax to update the views which simply checking results in database if there are results found, my html div will updated withsection in that div

I am trying to use ajax to update the views which simply checking results in database

if there are results found, my html div will updated with

section in that div if not found result, it will check after some time.

the code is working without if else section

but the client browser will crash at the certain point, the browser is frozen (seems if i change cache into True, it works fine, is that right?)

my if else statement using for judging if i should updating again. logic is if my html div got

segment i will stop updating.

<script>
        function updateResults() {
            $.ajax({
                type: 'GET',
                url: "/sResultsView",
                cache: true,
                success: function(html){
                    $("#resultsDiv").html(html);
                } 
            });
            window.setTimeout('updateResults()', 4000);
        }
        if($'#resu开发者_C百科ltsDiv:not(:has(p))'){
            updateResults();
        }else {
            return;
        }
        }
    </script>

however, it keep telling me syntax is wrong in if else, it is the first time i write ajax

any suggestion? am i doing right? or any better ideas?

many thanks in advance


Currently you're creating an infinite loop (or would be), instead schedule the check inside your success callback, like this:

function updateResults() {
  $.ajax({
    type: 'GET',
    url: "/sResultsView",
    cache: true,
    success: function(html){
      $("#resultsDiv").html(html);
      if($('#resultsDiv p').length == 0) //if not found, run again in 4 seconds
        window.setTimeout(updateResults, 4000);
    } 
  });
}

For completeness sake, your syntax error is in the if statement missing patenthesis, here:

if($('#resultsDiv:not(:has(p))')){
    ^ ------- missing --------- ^
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号