0

Please see my code below. I want to auto refresh a div on a php page. I tried to refresh through javascript and html header, but it is slowly slowing down my computer.

page2.php

<?php

if($_GET['type']!='ajax'){
    include 'header.php';
    echo "<div id='main-content'>";
}
?>
Itm 1</br>
Itm 2

<img class="ajax-loader" src="ajax-loader.gif" alt="loading..." />

<?php
if($_GET['type']!='ajax'){
    echo "</div>";
    include 'footer.php';
}?>

app.js

$.cergis = $.cergis || {};
$.cergis.loadContent = function () {
    $('.ajax-loader').show();
     $.ajax({
         url: pageUrl + '?type=ajax',
        success: function (data) {
            $('#main-content').html(data);
            // hide ajax loader
            $('.ajax-loader').hide();

  }
    });
    if (pageUrl != window.location) {
        window.history.pushState({ path: pageUrl }, '', pageUrl);
    }
}
$.cergis.backForwardButtons = function () {
    $(window).on('popstate', function () {
        $.ajax({
            url: location.pathname + '?type=ajax',
            success: function (data) {
                $('#main-content').html(data);
            }
        });
    });
}
$("a").on('click', function (e) {
    pageUrl = $(this).attr('href');
    $.cergis.loadContent();
    e.preventDefault();
});
$.cergis.backForwardButtons();

i have tried different variation but no luck. please help me.

thanks.

app.js changed...

function myTimer() {
  $('.ajax-loader').show();
  $.ajax({
      url: pageUrl + '?type=ajax',
      success: function (data) {
          $('#main-content').html(data);
          // hide ajax loader
          $('.ajax-loader').hide();
        }
  });

}

setInterval(function(){myTimer()}, 1000);
2
  • 'showing down'? you sure? Commented Jul 22, 2013 at 13:09
  • sorry its slowing down.... Commented Jul 22, 2013 at 13:13

2 Answers 2

1

Try setTimeout:

function myTimer() {
  $('.ajax-loader').show();
  $.ajax({
      url: pageUrl + '?type=ajax',
      success: function (data) {
          $('#main-content').html(data);
          // hide ajax loader
          $('.ajax-loader').hide();
          setTimeout(myTimer,1000);//so that the request ends setTimeout calls a new request.
      },
      error: function () {
          setTimeout(myTimer,1000);//If there is an error in the request the "autoupdate" can continue.
      }
  });
}
myTimer();//fire

this way setTimeout() waiting to finish the request to invoke a new request.

setInterval() does not wait, which makes simuntaneos generate multiple events, which causes the slowness.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use setTimeout($.cergis.loadContent, 1000); to refresh once or setInterval($.cergis.loadContent, 1000); to refresh each seconds (1000 milliseconds = 1second).

See http://www.w3schools.com/js/js_timing.asp

4 Comments

i tried both of them....actually this time its starts but not end the action...stuck in middle...
What these functions will do is start the function you give in parameter. So if it doesn't end it's because the function you give is not doing what you call "end the action".
i changed app.js at the end of original post...basically this ajax changed the div on click....what i want it should change auto...
$(function(){ setInterval(myTimer, 1000);} will start the interval when DOM is ready

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.