www.webdeveloper.com
Results 1 to 8 of 8

Thread: Auto refresh function in javascript

  1. #1
    Join Date
    Jun 2009
    Posts
    3

    Smile Auto refresh function in javascript

    I have .net 2.0 and javascript. I need to auto refresh a page from javascript function with 1 minute interval.
    I created a button and call a function from html button onload event

    timedRefresh(1000)

    function timedRefresh(timeoutPeriod) {
    setTimeout(function(){window.location.reload(true);},timeoutPeriod);

    Reload doesn't work.
    Please let me know what I'm doing wrong.
    Last edited by sbaltam; 06-10-2009 at 07:59 PM.

  2. #2
    Join Date
    Aug 2005
    Location
    Northampton, UK.
    Posts
    421
    Hey, there's nothing wrong with the code you've posted. But you haven't posted your whole code. So post the entire thing, and could you expand on what you actually want to do? because i'm a little stumped.

    (And why the button's onload?)

    Cheers,
    Jamey

  3. #3
    Join Date
    Jun 2009
    Posts
    3
    My user wants to click on button and set the auto refresh on and off.
    I created a button in .net 2.0 web page which created from master page.
    I created and HTML button, so on onload event I call the javascript function where I validate a date then I need to call refresh function to reload a page.
    On another button I need to cancel the refresh.
    IF you have a better solution please let me know.
    Thank you!!!

  4. #4
    Join Date
    Aug 2005
    Location
    Northampton, UK.
    Posts
    421
    Sounds like you need something server-side to store whether or not you actually want to refresh.... Though something like this would work:
    Code:
    <script type="text/css">
    
    var refresh;
    
    function cancelRefresh(){
      if(refresh){
        clearTimeout(refresh);
        refresh=false;
      }
    }
    
    function refresh(timeoutPeriod){
      refresh = setTimeout(function(){window.location.reload(true);},timeoutPeriod);
    }
    
    window.onload=refresh(1000);
    </script>
    ...more code here
    <input type="button" value="Cancel Refresh" onclick="cancelRefresh();" />
    Is that the sort of thing you're after?

    Cheers,
    Jamey

  5. #5
    Join Date
    Jun 2009
    Posts
    3
    Jamey,thank you for your reply. yes, the cancel refresh looks like what i need. But the refresh function doesnt work for me.
    when I click on the button, nothing happend. I expect refresh the page every minute.

  6. #6
    Join Date
    Mar 2009
    Location
    Maine USA
    Posts
    49
    timedRefresh(1000)
    Regarding the above line, 1000 (milliseconds) equals 1 second, not one minute. 60x that is 60000 milliseconds, which is one minute.

  7. #7
    Join Date
    Aug 2005
    Location
    Northampton, UK.
    Posts
    421
    I didn't touch the refresh button at all, just made a cancel refresh button and made the script start the timeout on the window's load... As coderic says, 60000 milliseconds=1 minute. It was refreshing every second.

    Cheers,
    Jamey

  8. #8
    Join Date
    Apr 2009
    Posts
    96
    why not use a cookie

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles