3
<script>

$(document).ready(function()
    {
        setInterval(doAjaxStuff, 500);
    }); 
    function doAjaxStuff()
    {
        /* $.ajaxSetup ({ cache: false  }); */  

        $.ajax
        ({
            url: "../getStatus/"+id,
            dataType: 'json',
            success: function(json) 
            {
                if(json.status ==  "ACTIVE")
                {
                    $('#ajaxStatus').html(jason.status);

                }
            });
         }

</script>


//I get refreshed and cause flickering
<p id= ajaxStatus > Refresh Me</p>
stackoverflow
  • 18,348
  • 50
  • 129
  • 196

5 Answers5

2

there is an easier to use jquery function to load text from a page. You could replace the whole .ajax call with $("#ajaxStatus").load("../getStatus/" + id)

Not sure if that stops the flickering but at least should make the program cleaner.

Rodolfo
  • 4,155
  • 23
  • 38
2

I had the same problem on using a jquery load();

$('#div').load('/ajax.php', data);

In the ajax.php that i loaded into the #div i included the jquery lib and my stylesheet again. so the new content got new style and jquery functions right?

Found out the new content picks up the parent css and js functions!

bottomline:

including same css in new loaded div as parent cause blinking because the html elements get styled again.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
frankey
  • 41
  • 1
1

Use this:

$( '#ajaxStatus' ).replaceWith( jason.status );
Kalamarico
  • 5,466
  • 22
  • 53
  • 70
Shaze
  • 792
  • 1
  • 9
  • 14
  • It won't stop flickering even if you change the interval timer, although correct way and good practice is to use setTimeout instead of setInterval. As in some cases, it has been found that it triggers race condition. The solution to your issue will be fixed if you use - .replaceWith() instead of .html(). This is known if you're trying to refresh any element on the page. – Shaze Sep 11 '17 at 09:36
  • Also just so you know what the .replaceWith does is - It only updates only the content. The replaceWith() method replaces selected elements with new content. – Shaze Sep 11 '17 at 09:45
1

Try to add a check so you only update if the value has changed.

if(json.status ==  "ACTIVE" && $('#ajaxStatus').html() !== "ACTIVE")
{
    $('#ajaxStatus').html(jason.status);        
}
Björn
  • 1,593
  • 1
  • 13
  • 28
1

I would say 500 is way too fast. You have to remember that the interval will keep going and requesting the info even if the ajax call is not completed. my suggestion is to either change setInterval to setTimeout and reinitialize the timeout on success, or crank up the interval to 1500-2000. You better off with the timeout though

locrizak
  • 12,192
  • 12
  • 60
  • 80