admin管理员组

文章数量:1296400

I want to implement a ajax call to a page every 3 seconds. It will either return 0 if false or a html snippet like <div>Content</div>

How should I proceed to place or remove that div on the page according to what ajax returns ?

I want to implement a ajax call to a page every 3 seconds. It will either return 0 if false or a html snippet like <div>Content</div>

How should I proceed to place or remove that div on the page according to what ajax returns ?

Share Improve this question asked Apr 9, 2014 at 4:15 bockziorbockzior 1991 gold badge6 silver badges21 bronze badges 1
  • 2 what you have tried ? – Aamir Shahzad Commented Apr 9, 2014 at 4:17
Add a ment  | 

2 Answers 2

Reset to default 3

Use setInterval()

setInterval(ajaxCall, 3000);

function ajaxCall() {
   $.ajax({url:url,
           type:'html',
           success:function(result){
             if(result==0)
               $('#content').hide();
              else
                $('#content').html(result).show();  

            }
        });
 }



 <div id="content">Content</div>

One possible way:

html

<div id="one" style="display:none"></div>
<div id="two" style="display:block"></div>

Now in your success function set the appropriate div visible or hidden

ajax.request
({
    // some code
    success: function(response)
    {
        // here check the answer and show the div with id one
        document.getElementById('one').style.display = 'block';
    }
})

本文标签: javascriptAJAX to showhide divStack Overflow