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 ?
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';
}
})
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>