Milad Pazouki
Milad Pazouki

Reputation: 1

Jquery setInterval works perfectly on Firefox , but did not work on IE8

i wrote a simple code to get content from PHP file and refresh it every 30 second . it worked pretty on FireFox , but in IE8 only load contents one time ! can any body help me to fix it ?!

This is my Code :

<script>

var content;
var temp = "something";

    $.get('refresh.php', function(data) {
    content = data;
    })
  .success(function() {
      if (temp != content) {
          $("#success").fadeOut(2000, function () 
          {
              $("#success").html(content).fadeIn(2000); 
          }
          ); // end .fadeOut
      temp = content;
  }

  }) //end .success

  .error(function() { $("#success").html("error"); });

var refreshId = setInterval(function()
{
    $.get('refresh.php', function(data) {
    content = data;
    })
    .success(function() {
    if (temp != content) {
        $("#success").fadeOut(2000, function () 
        {
            $("#success").html(content).fadeIn(2000); 
        }
        ); // end .fadeOut
    temp = content;
    }

  }) //end .success

  .error(function() { $("#success").html("error"); })

}, 27000);
  </script>

and on PHP code i have this code :

echo rand();

Upvotes: 0

Views: 840

Answers (3)

Gorodeckij Dimitrij
Gorodeckij Dimitrij

Reputation: 4636

IE cashing all get requests, try this:

$.get('refresh.php?'+ (new Date().getTime()), function(data) {
    content = data;
    })
...

Upvotes: 0

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

Avoid line breaks and comments in the mid method-chain; at best it's just ugly, at worst not all js engines will play ball. Also, you can move the declaration of content into the outer closure and cache the result of $("#success").

var refreshId = setInterval(function() {
    var $success = $("#success");
    var content;
    $.get('refresh.php', function(data) {
        content = data;
    }).success(function() {
        if (temp != content) {
            $success.fadeOut(2000, function () {
                $success.html(content).fadeIn(2000); 
            });
            temp = content;
        }
    }).error(function() { $success.html("error"); });
}, 27000);

Upvotes: 0

mikos
mikos

Reputation: 424

IE will be caching the ajax result. Put this before your $.get() calls:

$.ajaxSetup({
    cache: false
});

Upvotes: 1

Related Questions