www.webdeveloper.com
Results 1 to 2 of 2

Thread: Variable changed by comparison causing logic error.

  1. #1
    Join Date
    Feb 2009
    Posts
    2

    Question Variable changed by comparison causing logic error.

    Here is the function causing the trouble - I am trying to clean it up and make it more efficient/easier to read. When I make a change in the conditional statement it zeros the global variable
    Code:
    finishTime
    .

    by global - I mean it is declared by the statement
    Code:
    var finishTime=0;
    outside of any function, then set inside another function by the statement
    Code:
    finishTime=now.getTime()+(60*mins+secs)*1000+(cent*10);
    Code:
    function chrono()
    {
    	now=new Date();
    	var timeToRun=finishTime-now.getTime();
    	totalTime.setTime(timeToRun);
    	
    	mins=totalTime.getMinutes();
    	secs=totalTime.getSeconds();
    	cent=Math.floor(totalTime.getMilliseconds()/10);
    	display();	// prints out mins, secs and cent - it does not change them.
    	
    	if(timeToRun>100)	// go round the function again - this causes the trouble
    		{
    		counter=setTimeout("chrono()",5);
    		}
    	else	// leave the loop - countdown completed - this works all the time
    		{
    		document.getElementById("pinger").innerHTML=pinger;
    		clearTimeout(counter);
    		reset();
    		}
    	// alternative comparison that is less efficient but works! (cent>10 || secs!=0 || mins!=0)
    }
    Any suggestions much appreciated.

    Still thinking and still very puzzled, but putting this together has helped a little.

    PS testing in Firefox3.0.5, and the full html file is attached with all the javascript code.
    Attached Files Attached Files
    Last edited by ivanadventure; 02-18-2009 at 07:45 AM. Reason: typo

  2. #2
    Join Date
    Feb 2009
    Posts
    2

    sorted it our

    Seems that although it worked with the alternative condition, the error was in not setting the finalTime variable via the play function, before running the chrono function.

    All is resolved - the error was in the html! not in the javascript. Just very strange how the error shows with one condition and not the other!

    Thanks if you had a look.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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