0

I am trying to make a program that tests someones ability to learn different foreign words. For this I need to have a javascript function that hides a DIV and, fills an empty one, and then calls setTimeout(). The problem is that this function is not being called from what I can tell.

I have tried to find an answer online but couldn't find a solution that worked, maybe one of you guys can help. Here is my code:

<html>
<head>
 <style type="text/css">
 #starter
 {
 text-align: center;
 margin-top: 15%;
 }
 #main
 {
 text-align: center;
 z-index: -1;
 <?php
 $i = rand(1, 8); 
 switch($i) { 
 }
 ?>
 }
 </style>
 <script type="text/javascript">
 function weghalen(var element) {
 var element = document.getElementById('starter');
 element.parentNode.removeChild(element);
 }
 function volgendePagina() {
 window.location.href = "test.php";
 }
 function startOnderzoek() {
 weghalen('starter');
 var tekst = <?php include 'Scripts.php'; echoWoorden(); ?>;
 document.getElementById('mainP').innerHTML = tekst;
 setTimeout(volgendePagina(), 6000);
 }
 </script>
</head>
<body>
 <div id="starter">
 <p>
 Als u op 'Start onderzoek' druk begint uw timer van 6 seconden te lopen. Aan het einde van uw 6 seconden wordt u vanzelf naar een volgende pagina gebracht.
 </p>
 <a href="javascript: startOnderzoek()">
 <input type="button" value="Start onderzoek" />
 </a>
 </div>
 <div id="mainDIV">
 <p id="mainP"></p>
 </div>
</body>

Thanks in advance!

brenjt
16.3k14 gold badges80 silver badges120 bronze badges
asked Dec 29, 2014 at 17:31

3 Answers 3

1

Try changing your link to use an onclick:

<a href="javascript:void(0);" onclick="startOnderzoek(); return false;">

Also you need to change your setTimeout to this:

setTimeout(volgendePagina, 6000);

Having the parenthesis after volgendePagina will cause it to execute immediately and not when the 6 seconds are up.

Also in reference to what Paul said about the weghalen function. The parameter is named the same as the variable that you're creating.

You might want to consider refactoring the function like so:

function weghalen(child) {
 var element = document.getElementById('starter');
 element.parentNode.removeChild(child);
}

I am also not sure that the logic of weghalen will work as you expect.

answered Dec 29, 2014 at 17:36
Sign up to request clarification or add additional context in comments.

Comments

1

Check your JS console: you can't include var in a function parameter declaration. Change:

function weghalen(var element) {

to:

function weghalen(element) {
answered Dec 29, 2014 at 17:38

Comments

1

Try:

setTimeout(volgendePagina, 6000);

or

setTimeout(function(){ volgendePagina() }, 6000);

Also here:

function weghalen(var element) {
 var element = document.getElementById('starter');
 element.parentNode.removeChild(element);
}

Firstly, you do not include var in between the function's parenthesis. Secondly, you are creating a variable which has the same name as the function's parameter, so the language doesn't know which one you want to use where. Change it to something like this:

function weghalen(el) {
 var element = document.getElementById('starter');
 element.parentNode.removeChild(el);
}
answered Dec 29, 2014 at 17:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.