www.webdeveloper.com
Results 1 to 3 of 3

Thread: setTimeout auto reload

  1. #1
    Join Date
    Aug 2009
    Posts
    1

    Unhappy setTimeout auto reload

    Hi,

    I'm trying to make my script auto reload the page if the new value is higher then the old. The problem is that i'm a total bonehead when it comes to javascript and desperately need some help.

    This is how far I got:

    <script type="text/javascript">
    //if newvalue is more than oldvalue reload page.

    var oldvalue = 1;
    var newvalue = 2;
    var c=0
    var t

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

    function updcheck() {
    if (newvalue>oldvalue)
    {
    document.write("<b>New upd</b>");
    //window.location.href=window.location.href // reload page
    }
    </script>

  2. #2
    Join Date
    Apr 2008
    Posts
    157
    When you reload the page, all the javascript variables are deleted!

    Something like this will work:
    Code:
    var timer = window.setTimeout(function() {
    
    	var myvalue = 100;
    
    	function getURLParam(strParamName){
    		var strReturn = "";
    		var strHref = window.location.href;
    		if (trHref.indexOf("?") > -1 ) {
    			var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    			var aQueryString = strQueryString.split("&");
    			for (var iParam = aQueryString.length; iParam >= 0; iParam-- ){
    				if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ) {
    					var aParam = aQueryString[iParam].split("=");
    					strReturn = aParam[1];
    					break;
    				}
    			}
    		}
    		return unescape(strReturn);
    	}
    	var currentValue = getURLParam("currentValue");
    	if (currentValue > myvalue) location.href = location.href + "currentValue" + (++currentValue);
    	else window.clearTimeout(timer);
    }, 1000);
    It checks the current Value using the URL string, and if it's lower than the value you want, it reloads the page.

    I would actually prefer using iframe or something like that, it'd be much simpler.

  3. #3
    Join Date
    Feb 2009
    Posts
    22
    What you're looking for is window.location.reload();

    Code:
    function updcheck() {
    if (newvalue>oldvalue)
    {
    document.write("<b>New upd</b>");
    window.location.reload(); // reload page
    }

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