SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to autmatically refresh a page every 5 to 15 minutes without user knowing??

    Hi there,

    Is there away where i can refresh the page without the vistor using it for example i want to refresh the part of a function i guess you could call it refreshing the page but only for the displaying of function refresh();

    the kind of function i am looking for is somthing like this.

    PHP Code:
    REFRESH(page(),1000); 
    abit like javascripts set time out but is there one for PHP itself?

    Thanks,William

  2. #2
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use AJAX calls on a setInterval() timer or something. SitePoint articles cover working with the XmlHttp object.

    Cheers,
    D.

  3. #3
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can i get php to refresh the same page every 15 minutes in a loop with setinterval and javascript settimeout function?

  4. #4
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i found this in a tutorial which i am playing around with but how can i get it to display somthnig every minute non-stop?

    PHP Code:
    ?>
    <script type="text/javascript">
    var c=0;
    var t;
    var timer_is_on=0;

    function timedCount()
    {
    document.getElementById('output').value=c;
    c=c+1;
    t=setTimeout("timedCount()",6000);
    }

    function doTimer()
    {
    if (!timer_is_on)
      {
      timer_is_on=1;
      timedCount();
      }
    }
    </script>
    <?
    Is that possible??

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by william232 View Post
    can i get php to refresh the same page every 15 minutes in a loop with setinterval and javascript settimeout function?
    PHP is on the server, so no. You can use js to refresh the page, and use php via ajax calls.

  6. #6
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there away to refresh the page using javascript every 15 minutes in a continius loop?

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP aint gonna do it because once it leaves the server PHP loses control.
    You can use setInterval and an Ajax call (Javascript) to get new content to replace the current page.

    With jQuery:
    Code Javascript:
    function refresh_handler() {
        function refresh() {
           $.get('your-server-script.php', null, function(data, textStatus) {
               $("body").html(data);
            });
        }
        setInterval(refresh, 300*1000); //every 5 minutes
    }
     
    $(document).ready(refresh_handler);

    jQuery isn't my first language so that code might not be perfect, but should give you a starting point.
    Replace your-server-script.php with the URL of your server side script. It should return the HTML that will become the content of your <body> tag.
    If you just want to update part of the document change the selector from "body" to something more specific (e.g "#someElementID")

  8. #8
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is JQuery abit like ajax?

    also can you explain what does this part mean?

    $("body").html(data);

    is that where i want the data to be displayed??

  9. #9
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jQuery is a framework for JavaScript. It includes functionality for making AJAX calls.

    I really suggest you do a bit of Googling on the subjects. Don't expect people on forums to spoon-feed you every bit of information you need.

    Cheers,
    D.

  10. #10
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jQuery is a Javascript library that simplifies a lot of common JS programming tasks such as selecting elements in the DOM, handling events (click, mouseover), running Ajax transactions, doing animation etc.

    Ajax is the process of getting data from the server to the browser or page without reloading the whole page. jQuery provides functions to manage Ajax transactions (as do all JS libraries).

    $("body").html(data) means this:
    $ is the jQuery object (shortcut).
    "body" is a HTML selector (same as you might use in CSS). In this case it will get all <body> tags, of which there should be exactly 1.
    data will be the response from your server side script (the data of the ajax transaction) and the .html() function call will set the innerHTML of <body> to that data.

    That line of code will be called when the the Ajax transaction finishes, and it will set the content of <body> to be whatever your .php script displays.

  11. #11
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or maybe you can expect them to.

    Cheers,
    D.

  12. #12
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by disgracian View Post
    Or maybe you can expect them to.
    definitely here at Sitepoint - not sure whether I always like the helpfulness of Sitepoint members, it makes people lazy, and if you don't help as much as those regulars, you're seen as unfriendly...
    Dan G
    Marketing Strategist & Consultant

  13. #13
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Luckily I don't care about being perceived as unfriendly. But yes, the culture here does seem to make people entirely dependent on hand-outs which actually harms them rather than helps.

    Cheers,
    D.

  14. #14
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why do i get this error in the error console of firefox?

    Error: $ is not defined
    Source File: http://localhost/timerefresh.html
    Line: 11

    Line 11 is

    $(document).ready(refresh_handler);

    why do i get that error?

  15. #15
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you loaded the jquery library?

    BTW localhost doesn't work so well on the web :P

  16. #16
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no since i've never used it b4 is that why i am getting that error?

  17. #17
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ive downloaded it and tried to include it into my page and this is what i have atm

    HTML Code:
    <script type="text/javascript" src="jquery-1.3.2.min.js">
    function refresh_handler() {
        function refresh() {
           $.get('time.php', null, function(data, textStatus) {
               $("body").html(data);
            });
        }
        setInterval(refresh, 300*1000); //every 5 minutes
    }
    $(document).ready(refresh_handler);
    </script>
    i have no errors been shown but nothing is being displayed so why do i get a blank page?

    Thanks

  18. #18
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to close the first script and open another
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script type= ...>
    //your code here
    </script>

  19. #19
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ive included it but i am still getting this as an error

    Error: $ is not defined
    Source File: http://localhost/timerefresh.html
    Line: 11

    My code is

    HTML Code:
    <script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript">
    function refresh_handler() {
        function refresh() {
           $.get('time.php', null, function(data, textStatus) {
               $("body").html(data);
            });
        }
        setInterval(refresh, 300*1000); //every 5 minutes
    }
    $(document).ready(refresh_handler);
    </script>

  20. #20
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Why don't you just command the page to automatically refresh itself?

    You can place a meta command in the head section of the page in order to do so.

    15 x 60 = 900 seconds

    Code html4strict:
    <head>
        <meta http-equiv="refresh" content="900">
        ...
    </head>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  21. #21
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Start reading.

    Cheers,
    D.

  22. #22
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by disgracian View Post
    Start reading.

    Cheers,
    D.

    Why do i even get that error ive included the jquery javascript file havnt i?

  23. #23
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In Firebug go to the net panel, view all file types, and ctrl + F5 to refresh the page. Make sure your jquery.js script is loading (200 or 304 status). If you get a 404 the path to jquery is wrong.

    And there's no point linking to localhost here. Local host means your own computer, no one else can see it.

  24. #24
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Why don't you just command the page to automatically refresh itself?

    You can place a meta command in the head section of the page in order to do so.

    15 x 60 = 900 seconds

    Code html4strict:
    <head>
        <meta http-equiv="refresh" content="900">
        ...
    </head>
    Ill try that and see what happens thanks

  25. #25
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by william232 View Post
    Why do i even get that error ive included the jquery javascript file havnt i?
    Did you drop the jquery.js file in the same directory as the page you're debugging?

    Cheers,
    D.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •