Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Ajax Update

Options
  • 02-03-2010 1:15pm
    #1
    Registered Users Posts: 2,110 ✭✭✭


    Hi,
    I have a webpage which is being sent a variable from a c function. Th web page displays the variable fine but as it is constantly changing I need to refresh the page to see the new value.

    I have been looking intp xmlhttprequests and it seems perfect.
    All I want Ajax to do is request the page and display it with a refresh rate of 1 second without having to reload the entire page.

    Any hints?


    D


Comments

  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    I think you want to use the setTimeout() function


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    setInterval, combined with the get function.

    jQuery would make the call and the "replace content" pretty simple
    function refreshAjax() {
      $.get(url,function(data) {
       $("#targetElement").html(data)
      }
    }
    
    $(document).ready(function() {
      expr="refreshAjax()";
      setInterval(expr,1000)
    })
    


  • Registered Users Posts: 2,110 ✭✭✭sei046


    Hmmm.
    Ok, so lets say Im on this page Sensors.stm.
    If I include " %! adc " the page will input a table which is formed inside a c function.

    What I want is a realtimeupdate of this table. To do this the html page sensors has to be refreshed. I dont want this. All I want to do is let the javascript update the variable, then display it.

    Confusing to explain! Thanks for reply guys


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    1 second is quite fast as it's the client doing the call each time. I would re-write that JS so that the call to refresh the data is only done once a previous cycle is completed. So in effect there is a 1 sec + (x) timeout where x is a network delay or whatever.
    function refreshAjax() {
      $.get(url,function(data) {
       $("#targetElement").html(data);
       setTimeout("refreshAjax()", 1000);
      }
    }
    
    $(document).ready(function() {
      refreshAjax();
    })
    


  • Registered Users Posts: 2,110 ✭✭✭sei046


    ok, will check out that code. This is for data display so there wont be any interaction. I dont have to worry about a consumer!


  • Advertisement
Advertisement