简体   繁体   中英

Javascript - run a php file every X seconds

I would like to be able to record the amount of seconds a person is on my site in the simplest way possible. Don't need google analytics or any other 3rd party sources.

The php script would create a connection to mysql and update the relevant values,

I found some script online, but it doesn't seem to be working:


<script  src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<script>
   $(document).ready(function()
   {
       var refreshId = setInterval(function()
          $.load('timeonpage.php?wzx=<?php echo $t; ?>&ip=<?php echo $ip; ?>');
       }, 5000);
   );
</script>

Thanks for any help!

There is a syntax error, your missing an opening to the setInterval function, and the closing of the .ready method

<script  src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
    var refreshId = setInterval(function(){ //SYNTAX ERROR HERE
        $.load('timeonpage.php?wzx=<?php echo $t; ?>&ip=<?php echo $ip; ?>');
    }, 5000);
}); // SYNTAX ERROR HERE
</script>

Additionally, I would recommend using the $.get method, as .load in JQuery is typically meant for loading HTML into a particular container:

var refreshId = setInterval(function(){
    $.get('timeonpage.php',{wzx:<?php echo $t?>,ip:<?php echo $ip?>});
},5000);
<script  src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<script>
$(document).ready(function()
{
 var refreshId = setInterval(function(){ //ANOTHER ERROR
    $.load('timeonpage.php?wzx=<?php echo $t; ?>&ip=<?php echo $ip; ?>');
 }, 5000);
}); //ERROR ON THIS LINE
</script>

Well you're missing a '}' in there on the closing of document ready. Try that, might be something so simple, report back if it's not and will have another look at it :)

Also: If you'd consider a 3rd party service, take a look at GoSquared , seriously amazing site there :)

Edit: Another error on setInterval line :)

With your current method, a user might be counted as online for all 86,400 seconds in a day if they just leave their browser window open.

Personally, my site is set up such that every time a user actually loads a page, their "last page loaded" time is updated. Then, a cron script runs every minute and checks to see which users have loaded a page in that last minute. Counting those up allows me to determine reasonably well how much time each user has spent online.

Additionally, this would cause WAY less load on your server - your solution would probably kill it if you had more than 50 or so users on your site.

it would probably be a lot more efficient onLoad to call an Ajax script that saves the page and any other data you want to the database and then returns a unique id which you store in a variable. On the onbeforeunload event you could then make another call to the database with the unique id. Just my two cents...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM