www.webdeveloper.com
Results 1 to 11 of 11

Thread: Timer problem

  1. #1
    Join Date
    Nov 2009
    Posts
    65

    Timer problem

    Hey,

    Code:
    function start_spel(){
    index = document.getElementById("bewerking").selectedIndex;
    document.getElementById("form").reset();
    document.getElementById("bewerking").selectedIndex = index;
    timer = window.setTimeout("einde()",60000);
    opgave_ophalen();	
    }
    
    function controleer(){
    antwoord = document.getElementById("antwoord");
    oplossing = eval(document.getElementById("opgave").value); //String behandelen als integer/float en output is ook integer/float.
    if(timer != ""){
    	if(antwoord.value == oplossing){
    		document.getElementById("juist").value++;
    		}
    	else{
    		document.getElementById("fout").value++;
    		}	
    	opgave_ophalen(); //na he evalueren en bijvoegen van de correcte score-verdeling wordt opgave_ophalen() telkens opnieuw aangeroepen zodat er ook telkens een nieuwe opgave verschijnt. na 1 min wordt de "manuele lus" doorbroken d.m.v een string.
    	}
    else{
    	alert("Klik eerst op de knop \"Start het spel\"");
    	}
    }
    This processes an error with the (timer!="") statement/structure.

    Error: Expected ';' on line 43
    Code:
    if(timer != ""){
    Any idea why this does not work?

    the full code you can find here(at the bottom of the first post):http://www.webdeveloper.com/forum/sh...d.php?t=224635

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,653
    write in var timer = ""; as the first line of this script

  3. #3
    Join Date
    Nov 2009
    Posts
    65
    I have tried defining the timer at the top as var timer = ""; but this did not solve the problem. I have however mistaken line 44 with 43. The actual line is:
    Code:
    oplossing = eval(document.getElementById("opgave").value);
    I have tried splitting the line up in a line getting the value and another applying the eval() method to it but the same error occured.
    Last edited by ajajajak; 02-15-2010 at 09:49 AM.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,653
    Code:
    oplossing = eval(document.getElementById("opgave").value);
    Definition and Usage

    The eval() function evaluates and/or executes a string of JavaScript code.

    First, eval() determines if the argument is a valid string, then eval() parses the string looking for JavaScript code. If it finds any JavaScript code, it will be executed.


    so what does eval() execute or evaluate there?

  5. #5
    Join Date
    Nov 2009
    Posts
    65
    Well it should evaluate the string composed out of 2 random numbers generated between 1 - 10 and an assigned calculation sign.

    e.g
    Code:
    var getal1 = getal(); //getal() being the random generator function
    var getal2 = getal();
    bewerking = "+" 
    
    opgave.value = getal1 + " " + bewerking + " " + getal2;
    opgave.value would eventually look something like:"2 + 2" in stringformat but I would need to be able to evaluate what the calculation produces to an answer inputted by the user. So in our example 2+2 = 4 would give a correct answer and not 22.

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,653
    i took the whole page code from here (http://www.webdeveloper.com/forum/sh...d.php?t=224635). then (like i said before) i put the line "var timer = "";" as the first line of your script and after doing this there was no any error.

    here is the code:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Rekenspel</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="Rekenspel.css" />
    <script type="text/javascript">
    /* <![CDATA[ */
    var timer = "";
    
    function willekeurig(){
    getal = Math.random()*10;
    getal = Math.round(getal);
    return getal;
    }
    
    function opgave_ophalen(){
    index = document.getElementById("bewerking").selectedIndex;
    bewerking = document.getElementById("bewerking").options[index].value;
    getal1 = willekeurig();
    getal2 = willekeurig();
    opgave = document.getElementById("opgave");
    opgave.value = getal1 + " " + bewerking + " " + getal2;
    document.getElementById("antwoord").value = "";
    document.getElementById("antwoord").focus();
    }
    
    function einde(){
    alert("De minuut is verstreken. Controleer je score onderaan!");
    document.getElementById("opgave").value = "De test is afgelopen";
    }
    
    function start_spel(){
    document.getElementById("form").reset();
    timer = window.setTimeout("einde()",60000);
    opgave_ophalen();	
    }
    
    function controleer(){
    antwoord = document.getElementById("antwoord");
    oplossing = eval(document.getElementById("opgave").value);
    if(timer != ""){
    	if(antwoord.value == oplossing){
    		document.getElementById("juist").value++;
    		}
    		else{
    			document.getElementById("fout").value++;
    			}	
    		opgave_ophalen();
    	}
    else{
    	alert("Klik eerst op de knop \"Start het spel\"");
    }
    }
    /* ]]> */
    </script>
    </head>
    <body>
    <div id="wrapper">
    <form id="form" action="">
    	<fieldset>
    		<legend>Rekenspel</legend>
    		<fieldset id="one">
    			<div id="block1">
    				<p>Kies de bewerking:</p>
    				<select id="bewerking">
    					<option value="+">Optellen</option>
    					<option value="-">Aftrekken</option>
    					<option value="*">Vermenigvuldigen</option>
    				</select>
    			</div>
    			<div id="block2">
    				<input type="button" class="btn" id="orange" value="Start het spel" onclick="start_spel()" />
    				<input type="reset" class="btn" value="Begin opnieuw" onclick="window.clearTimeout(timer);" />
    			</div>
    		</fieldset>
    		<fieldset id="two">
    			<div id="block3">
    				<p>Opgave</p>
    				<input type="text" id="opgave" value="0" onfocus="blur()" />
    				<p>Antwoord</p>
    				<input type="text" id="antwoord" value="0" />
    				<input type="button" id="controle" class="btn" value="Controleer het antwoord" onclick="controleer()" />
    			</div>
    			<div id="block4">
    				<p>Score</p>
    				<label>Juist:</label><input type="text" class="left" id="juist" size="3" value="0" />
    				<label id="lblfout">Fout:</label><input type="text" class="left" id="fout" size="3" value="0" />
    			</div>
    		</fieldset>
    	</fieldset>
    </form>
    </div>
    </body>
    </html>

  7. #7
    Join Date
    Nov 2009
    Posts
    65
    I know. It doesn't throw an error but the alert doesn't show when the timer ends.

    This does not function:
    Code:
    if(timer != ""){
    	...
    else{
    	alert("Klik eerst op de knop \"Start het spel\"");
    }
    Try starting the game, fill in some numbers and wait for the game to end(alert should pop).
    Now press the button "Controleer het antwoord" once again. It should throw an alert("Klik eerst op de knop \"Start het spel\"") but it doesn't. This is the problem.

    Thank you btw for taking the time to look at my code, cheers!

  8. #8
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    I do not see any use of timer that can't be accomplished with a boolean variable. Try making a variable to keep track of the timer... set when started, clear when ended. setInterval can be assigned to a variable, but I'm not sure if setTimeout can.

  9. #9
    Join Date
    Nov 2009
    Posts
    65
    It can. I have used it in several other occassion before. The problem lies not with the timer but with the eval() function although I have no idea what is wrong with it. Fang suggested a solution(http://www.webdeveloper.com/forum/sh...d.php?t=224635) in the other thread but it was too vague. Maybe someone can explain why exactly the eval does not work.

  10. #10
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    You might need to make the assignment inside of the eval function:
    PHP Code:
    eval("somenumber = "+a+"+"+b+";"); 

  11. #11
    Join Date
    Nov 2009
    Posts
    65
    Just to make sure: you are talking about javascript right? I see you are using php code formatting so I wondered.

    You might need to make the assignment inside of the eval function:
    Err...How could I do this when the construction of what needs to be evaluated needs to be separated in another function?

    construction of calculation
    Code:
    function opgave_ophalen(){
    ...
    opgave.value = getal1 + " " + bewerking + " " + getal2;
    ...
    }
    evaluation
    Code:
    function controleer(){
    antwoord = document.getElementById("antwoord");
    oplossing = eval(document.getElementById("opgave").value);
    if(timer != ""){
    	if(antwoord.value == oplossing){
    		document.getElementById("juist").value++;
    		}
    		else{
    			document.getElementById("fout").value++;
    			}	
    		opgave_ophalen();
    	}
    else{
    	alert("Klik eerst op de knop \"Start het spel\"");
    }
    I don't understand why it is such a problem, what is going wrong? I'm evaluating a valid command via a correct variable call so...? Can anyone elaborate?
    Last edited by ajajajak; 02-16-2010 at 05:27 PM.

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