Page 1 of 4 123 ... LastLast
Results 1 to 10 of 37

Thread: How To Setup Auto Refresh That Can Be Turned On And Off

  1. #1
    Join Date
    Nov 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How To Setup Auto Refresh That Can Be Turned On And Off

    Hi guys ... I currently use an AUTO REFRESH feature on my stocktriggers.com (stock trading) website that works fanstastic. HOWEVER, I would like to setup a script that allows users to either TURN OFF / TURN ON, or manually ADJUST the auto refresh time within their browsers. Right now my main forum page auto refreshes every 60 seconds, and I can adjust it to whatever time I set on my end. But they have no control over it on their end ... and those with slower connections to the internet want the browser to refresh perhaps every 2 minutes, or every 5 minutes, for example. And some don't want the feature to be on at all, and prefer to refresh when they feel like. But at least half of my members like the current auto refresh feature the way it is now. So I would like to set it up so that everyone has an option to adjust the REFRESH TIME and / or TURN IT OFF AND ON on their end as they please.

    Let me know if there is a solution out there. I have searched everywhere on the net and can't seem to find an answer.

    Thanks, Wize

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Hopefully, someone who knows how to write proper cookie code will come along and offer suggestions as to how to improve this (or, if you are in a learning mood check here ) but, this is the basic idea (it may interfere with other cookies and/or be frustrated by other cookies on the page):

    Code:
    <script type="text/javascript">
    var reIt
    
    function doit(){
    if (window.location.reload)
    window.location.reload( true );
    else if (window.location.replace)
    window.location.replace(unescape(location.href))
    else
    window.location.href=unescape(location.href)
    }
    
    function startUp(){
    if (unescape(document.cookie).split(';')[1])
    reIt=setTimeout("doit()", unescape(document.cookie).split(';')[1])
    else
    reIt=setTimeout("doit()", 60000)
    }
    
    function setRe(val){
    clearTimeout(reIt)
    if (val==0){
    document.cookie=''
    return;
    }
    else
    document.cookie=val*1000
    startUp();
    }
    
    onload=startUp
    </script>
    HTML Code:
    <span>set refresh rate:</span>
    <input type="button" value="1 Minute" onclick="setRe(60);">
    <input type="button" value="2 Minute" onclick="setRe(120);">
    <input type="button" value="5 Minute" onclick="setRe(300);">
    <input type="button" value="No Refresh" onclick="setRe(0);"> <span>Default is 1 Minute</span>
    Works here in local trials in FF and IE. It is a session only cookie and so, must be set each browser session. It applies to the page it is on only.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John ... I like the script, and it works ... HOWEVER, my test webpage is jumping all over the places as if the script is running full time, in a loop or whatever. Also the TITLE BAR is flashing data as if it's refreshing constantly, etc. even though it does refresh only at the time interval set. I'm not sure what the cause is.

    The script I am currently using is the AUTO REFRESH script from Dynamic Drive. I love it, it works perfectly. But it would be awesome to be able to turn that script on and off as desired, and / or give the user the ability to adjust the refresh interval within their own browser, rather than me controlling it on my end. I have it set for 60 second refreshes but many members wish to turn it off from time to time, or adjust it to refresh every two minutes, or five minutes, etc.

    Thanks much, Wize

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Did you take the other script off of your page first before testing? Also, I couldn't be sure if I found the DD script you are talking about:

    Warning: Please include a link to the DD script in question in your post. See this thread for the proper posting format for asking a question.
    PLEASE: Include the URL to your problematic webpage that you want help with.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John ... yes, I tested it on a blank web page first and it reacted strangely, although it did work. So I didnt incorporate into my website.

    Here's the script I am currently using: http://www.dynamicdrive.com/dynamicindex6/refresh.htm

    Here's where I was testing the script you posted earlier:
    http://www.stocktriggers.com/set_refresh_rate.htm

    I recently built a script to HIDE / VIEW the members on my website at any given time, and it works great. But I was also hoping to build a script that allows REFRESH options on the user-end, and I can't seem to get exactly what I need.

    Thanks for your help!!

    Wize

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, your demo works fine here. Perhaps, on your end, it is suffering from what I mentioned about interaction with other cookies from your site/page(s) already in effect. I took my own advice and studied the info on cookies. I've worked out a more robust version that will persist for one week (persistence length is configurable as a comment in the code documents). It will also be in effect for all pages on your site that have this script on it, and should not interact with other established cookies:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title>User's Choice Refresh Rate - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <script type="text/javascript">
    
    function createCookie(name,value,days)
    {
    	if (days)
    	{
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name)
    {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++)
    	{
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }
    
    function eraseCookie(name)
    {
    	createCookie(name,"",-1);
    }
    
    
    var reIt
    
    function doit(){
    if (window.location.reload)
    window.location.reload( true );
    else if (window.location.replace)
    window.location.replace(unescape(location.href))
    else
    window.location.href=unescape(location.href)
    }
    
    function startUp(){
    // uncomment below line for testing only
    //alert(readCookie('resetInt'))
    if (readCookie('resetInt')!=null)
    reIt=setTimeout("doit()", readCookie('resetInt'))
    else
    return;
    }
    
    function setRe(val){
    clearTimeout(reIt)
    if (val==0){
    eraseCookie('resetInt')
    return;
    }
    else
    // 7 in the below line is the number of days persistence
    createCookie('resetInt', val*1000, 7)
    startUp();
    }
    
    onload=startUp;
    
    </script>
    <span>Set refresh rate:</span>
    <input type="button" value="1 Minute" onclick="setRe(60);">
    <input type="button" value="2 Minute" onclick="setRe(120);">
    <input type="button" value="5 Minute" onclick="setRe(300);">
    <input type="button" value="No Refresh" onclick="setRe(0);"><span><sub>(default)</sub><br>Your choice will persist site wide for 1 week unless changed or you clear your browser's cookies.</span>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Nov 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Awesome thanks ... much better!! I think the other script was reacting to the godaddy.com server or something, because it was on a blank page when tested, but it was reacting oddly. But this new script works great. I just need to see if I can incorporate a TIMER into it so users have a count-down on display before the next refresh occurs. I also want to convert this to a "drop down box" type of options instead of buttons, to take up less space on the page. If you know how to do that quickly ... let me know. Thanks again ... great stuff John.

    Wize

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think I got all that, the neat part is that the cookie will also set the select box to the currently active refresh interval, you can test that by setting it with the previous version then opening this one (for a local demo, both pages may need to be in the same directory, if live, the effect should be sitewide for pages with this script and markup on them:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title>User's Select Refresh Rate w/counter - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
    
    function createCookie(name,value,days)
    {
    	if (days)
    	{
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name)
    {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++)
    	{
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }
    
    function eraseCookie(name)
    {
    	createCookie(name,"",-1);
    }
    
    
    var reIt, countRe
    
    function doit(){
    if (window.location.reload)
    window.location.reload( true );
    else if (window.location.replace)
    window.location.replace(unescape(location.href))
    else
    window.location.href=unescape(location.href)
    }
    
    function startUp(){
    // uncomment below line for testing only
    //alert(readCookie('resetInt'))
    clearInterval(countRe)
    if (readCookie('resetInt')!=null){
    document.getElementById('reCounter').innerHTML=readCookie('resetInt')/1000
    var opts=document.getElementById('selRe').options
    for (var i_tem = 0; i_tem < opts.length; i_tem++)
    if (opts[i_tem].value==readCookie('resetInt')/1000)
    opts.selectedIndex=i_tem
    countRe=setInterval("document.getElementById('reCounter').innerHTML=parseInt(document.getElementById('reCounter').innerHTML, 10)-1", 1000)
    reIt=setTimeout("doit()", readCookie('resetInt'))
    }
    else
    return;
    }
    
    function setRe(val){
    clearTimeout(reIt)
    if (val==0){
    clearInterval(countRe)
    document.getElementById('reCounter').innerHTML='<b style="color:red;background-color:white;font-weight:normal;">Refresh Disabled</b>'
    eraseCookie('resetInt')
    return;
    }
    else
    // 7 (or the last value) in the below line is the number of days persistence
    createCookie('resetInt', val*1000, 7)
    startUp();
    }
    
    onload=startUp;
    
    </script>
    </head>
    <body>
    
    <span>Set refresh rate:</span><br>
    <select id="selRe" onchange="setRe(this.options[selectedIndex].value)">
    <option value="0" selected>No Refresh</option>
    <option value="60">1 Minute</option>
    <option value="120">2 Minute</option>
    <option value="300">5 Minute</option>
    </select>
    <span><br>Your choice will persist site wide on pages with the above select box<br>for 1 week unless changed or you clear your browser's cookies.<br>There are <span id="reCounter"><b style="color:red;background-color:white;font-weight:normal;">Refresh Disabled</b></span> seconds left to refresh.</span>
    
    </body>
    </html>
    Last edited by jscheuer1; 11-18-2005 at 11:02 PM. Reason: minor enhancement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    As sometimes happens, I got quite carried away with this one. You may prefer my version posted just before this post. It has almost all the functionality of this version but is in a much rougher form that might be easier to highly customize, though this version should be easier to just slip in wherever you want it and has the advantage of leaving no footprint on the page if javascript is disabled and should work in older IE versions:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title>User's Select Refresh Rate w/counter - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <style type="text/css">
    
    /* Begin Styles for User's Select Refresh Rate w/counter */
    
    #reCounter b { /* Set Style for 'Refresh Disabled' Message */
    color:red;
    background-color:white;
    font-weight:normal;
    }
    #refreshCounter { /* Set these two styles for Select Refresh Counter Container */
    border-collapse:collapse;
    border:1px solid black;
    font-family:tahoma,verdana,arial,sans-serif;
    font-size:.9em;
    }
    #refreshCounter td {
    padding:10px;
    }
    #preCount { /* These three Styles should remain unchanged */
    visibility:hidden;
    }
    #preC, #sufC {
    display:none;
    }
    
    /* End Styles for User's Select Refresh Rate w/counter */
    
    </style>
    </head>
    <body>
    <script type="text/javascript">
    
    /*User's Select Refresh Rate w/counter - script
       John Davenport Scheuer as first seen in
      http://www.dynamicdrive.com/forums
      under user name: jscheuer1
      This credit must remain on your page
      for legal use  */
    
    //Set number of days persistence for Cookie
    var pDays=7
    
    //NOTE: HTML allowed in Set Strings
    //Put text all on one line
    
    //Set String for select header:
    var selHead="Set Page Refresh Frequency:<br>"
    
    //Set String for Explanation of this setting to users:
    var reExp="<br>Your choice will persist for "+pDays+" Days, site wide, on pages displaying the<br>above select box, unless changed or you clear your browser's cookies.<br>"
    
    //Set String for Refresh Disabled:
    var reDis="Refresh Disabled"
    
    //Set String for counter prefix text:
    var prefixC="There are"
    
    //Set String for counter suffix text:
    var suffixC="seconds left until page refresh."
    
    /////////////////////Stop Editing Script///////////////////
    
    //Begin http://www.quirksmode.org Cookie Code:
    
    function createCookie(name,value,days)
    {
    	if (days)
    	{
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name)
    {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++)
    	{
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }
    
    function eraseCookie(name)
    {
    	createCookie(name,"",-1);
    }
    
    //End http://www.quirksmode.org Cookie Code
    
    var reIt, countRe, reCounter, text1, text2, opt
    
    function doit(){
    if (window.location.reload)
    window.location.reload( true );
    else if (window.location.replace)
    window.location.replace(unescape(location.href))
    else
    window.location.href=unescape(location.href)
    }
    
    function startUp(){
    text1=document.getElementById? document.getElementById('preC').style : document.all['preC'].style
    text2=document.getElementById? document.getElementById('sufC').style : document.all['sufC'].style
    clearInterval(countRe)
    if (readCookie('resetInt')!=null){
    opt=(readCookie('resetInt')/1000).toString(10).length
    text1.display=text2.display='inline'
    reCounter=document.getElementById? document.getElementById('reCounter') : document.all['reCounter']
    reCounter.innerHTML=padTo(readCookie('resetInt')/1000)
    var opts=document.getElementById? document.getElementById('selRe').options : document.all['selRe'].options
    for (var i_tem = 0; i_tem < opts.length; i_tem++)
    if (opts[i_tem].value==readCookie('resetInt')/1000)
    opts.selectedIndex=i_tem
    countRe=setInterval("reCounter.innerHTML=padTo(parseInt(reCounter.innerHTML, 10)-1)", 1000)
    reIt=setTimeout("doit()", readCookie('resetInt'))
    }
    else
    return;
    }
    
    function padTo(val){
    var val=val.toString(10)
    var preCount=document.getElementById? document.getElementById('preCount') : document.all['preCount']
    preCount.innerHTML=val.length==1&&opt==3? '00' : (val.length==2&&opt==3)||(val.length==1&&opt==2)? '0' : ''
    return val
    }
    
    function setRe(val){
    clearTimeout(reIt)
    if (val==0){
    text1.display=text2.display='none'
    clearInterval(countRe)
    reCounter.innerHTML='<b>'+reDis+'</b>'
    eraseCookie('resetInt')
    return;
    }
    createCookie('resetInt', val*1000, pDays)
    startUp();
    }
    
    onload=startUp;
    
    with (document){
    write('<table id="refreshCounter"><tr><td><span>'+selHead+'</span>')
    write('<select id="selRe" onchange="setRe(options[selectedIndex].value)">')
    write('<option value="0" selected>No Refresh</option>')
    write('<option value="60">1 Minute</option>')
    write('<option value="120">2 Minute</option>')
    write('<option value="300">5 Minute</option>')
    write('</select>')
    write('<span>'+reExp+'<span id="preC">'+prefixC+' </span><span id="preCount"></span><span id="reCounter"><b>'+reDis+'</b></span><span id="sufC"> '+suffixC+'</span></span>')
    write('</td></tr></table>')
    }
    
    </script>
    </body>
    </html>
    Last edited by jscheuer1; 11-19-2005 at 07:47 PM. Reason: fix minor Opera bug
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have a question: How do I change the default value of that script jscheuer1 posted? For example, when I enter the page, I'd like the script to start refreshing the page automatically every minute (without selecting it from the drop-down menu).

    I've already tried to do this
    Code:
    <option value="60" selected>1 Minute</option>
    but it doesn't work.

    Thanks for any help!
    Last edited by jamesd2; 10-12-2010 at 05:17 PM.

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
  •