简体   繁体   English

Javascript-每X秒运行一个php文件

[英]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. 不需要Google Analytics(分析)或任何其他第三方资源。

The php script would create a connection to mysql and update the relevant values, php脚本将创建与mysql的连接并更新相关值,

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 语法错误,您缺少setInterval函数的开头,而.ready方法的结尾

<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: 另外,我建议使用$.get方法,因为JQuery中的.load通常是指将HTML加载到特定容器中:

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 :) 另外:如果您考虑使用第三方服务,请访问GoSquared这个非常令人惊叹的网站:)

Edit: Another error on setInterval line :) 编辑:setInterval行上的另一个错误:)

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. 使用您当前的方法,如果用户仅打开浏览器窗口,那么一天之内可能会被视为整个86,400秒在线。

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. 然后, cron脚本每分钟运行一次,并检查最后一分钟哪些用户加载了页面。 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. 此外,这将减少服务器上的WAY负载-如果您的站点上有50多个用户,则解决方案可能会杀死它。

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. 调用一个Ajax脚本将页面和您想要的任何其他数据保存到数据库,然后返回存储在变量中的唯一ID,可能会比onLoad效率更高。 On the onbeforeunload event you could then make another call to the database with the unique id. 然后,在onbeforeunload事件中,您可以使用唯一的ID再次调用数据库。 Just my two cents... 只是我的两分钱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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