开发者

How to start a jQuery function after executing all the MySQL queries in that PHP page?

I have a JavaScript timer function which contains a jQuery function, and the JS function is called for every 3000 milliseconds.

<script type="text/javascript">
      function timer(){ $("div#display_time").load("timer.php");}

     var t=setInterval("timer()",30000);
</script>

Inside the body tag I have some MySQL update queries. I am triggering this by using onload attribute to the body tag, but these queries are not getting executed before the function is called.

<body onload="timer()">
<?php
........................
.......................
 //here we will be using o,o,o,7,7,2011 as the standard of our time...
 $t=((time())-(gmmktime(0,开发者_C百科0,0,7,7,2011)));
     $sql="UPDATE users
         SET
         user_level=1,
         current_qno=1,
         start_time=".$t.
      "WHERE user_id=".htmlentities($_SESSION['user_id']);
      $result=mysql_query($sql);
 if(!$result)
 {
    echo'<p>some thing went wrong' .mysql_error(). '</p>';
 }   
 $_SESSION['user_level']=1;
 **echo'<div id="display_time" "></div>';**


First thing to try, put your javascript at the very bottom of the page, right above the < /body> tag. It's recommended to put javascripts there anyways.

Next thing I'd try is to wrap all your javascript with:

$(document).ready(function() { // ensure the page has fully loaded before doing anything
    // put your code here
});

Let me know if either of those worked, but I might not understand your question fully.


First try changing:

var t=setInterval("timer()",30000);

to

var t = setInterval(timer,30000);

You want to make sure you're passing the function itself to the timer, not the return result from calling the function.

See http://jsfiddle.net/jfriend00/mhEJG/ for an example.

If that doesn't fix it, then you'll have to show us what the SQL statements actually put in the page. They look to me like they're server-side right? So, they have to get executed before the onload of the document happens. If they aren't putting the right stuff in the page, then that is an entirely server-side thing that has nothing to do with your client-side javascript. You will need to fix that call to setInterval though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜