0

I'm having a weird problem where whenever I go to my website It runs perfectly, but if I try and go to that same website again it won't load. If i wait about 1-2 minutes and go to the website again it will work. I believe it has something to do with either my jquery or my javascript setTimeout loop but i can't figure out how to fix it. Can anyone help me?

here is my code

  <html>
   <head>
   <head>
   <script type="text/javascript" src="jquery.js"></script>
   <script type="text/javascript">
    var question=1;
     function get(){

    $.post('ref.php', {num: question}, 
        function(output){
        $('#choice1').html(output).show();  
        }
    )
    $.post('ref2.php', {num: question}, 
        function(output){
        $('#choice2').html(output).show();  
        }
    )
    $.post('ref3.php', {num: question}, 
        function(output){
        $('#choice3').html(output).show();  
        }
    )
    $.post('ref4.php', {num: question}, 
        function(output){
        $('#choice4').html(output).show();  
        }
    )

    setTimeout('get()',1000);//THIS IS WHERE I THINK THE ERROR IS

    }


    </script>

    </head>

    </head>
    <body onload="get()" style="text-align:center; margin-top:20px; background-color:#000000">
    <p style="position:absolute;
    left:20px;
    top:110px;
    color:WHITE;
    font-size:30px;">Question Answer Grid</p>

    <p style="position:absolute;
    left:20px;
    top:200px;
    color:GREEN;
    font-size:24px;">Choice A:</p>

    <p id="choice1" style="position:absolute;
    left:140px;
    top:200px;
    color:GREEN;
    font-size:24px;"></p>

    <p style="position:absolute;
    left:20px;
    top:260px;
    color:BLUE;
    font-size:24px;">Choice B:</p>

    <p id="choice2" style="position:absolute;
    left:140px;
    top:260px;
    color:BLUE;
    font-size:24px;"></p>

    <p style="position:absolute;
    left:20px;
    top:320px;
    color:RED;
    font-size:24px;">Choice C:</p>

    <p id="choice3" style="position:absolute;
    left:140px;
    top:320px;
    color:RED;
    font-size:24px;"></p>

    <p style="position:absolute;
    left:20px;
    top:380px;
    color:YELLOW;
    font-size:24px;">Choice D:</p>


    <p id="choice4"style="position:absolute;
    left:140px;
    top:380px;
    color:YELLOW;
    font-size:24px;"></p>
    </body>
    </html>

And the php files:

  <?php
        $connect=mysql_connect("****","*****","*****");    
    mysql_select_db("*****");
    $num=$_POST['num'];
    $query=mysql_query("SELECT * FROM questions WHERE number='$num'");
    $query2=mysql_fetch_array($query);
    echo $query2['q1'];
    ?>

everything works except for that one error

4
  • Try to remove the single quetoes around get() function (Where the setTimeout is called) Commented Jan 13, 2014 at 3:41
  • see plnkr.co/edit/pqdEK97KMmwqOEAwgsGm?p=preview Commented Jan 13, 2014 at 3:54
  • when your website dosen't load does it gives any kind of error in console? Commented Jan 13, 2014 at 4:02
  • @guleria is right: can you paste server output (maybe generated HTML structure and console output?) in case when website doesn't work? Every second you make 4 POST requests to the server - it shouldn't overload your server but everything's possible. Or maybe there's some error while fetching data from db? You don't have any error checking there. Commented Jan 13, 2014 at 4:15

1 Answer 1

1

Change:

setTimeout('get()',1000)

to this:

setTimeout(get,1000)

get is a function. You don't need ' and () here.

Sign up to request clarification or add additional context in comments.

1 Comment

it is perfectly fine... as setTimeout() takes a string as an argument and executes it in the global scope

Your Answer

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.