我怎样才能运行PHP脚本与计时器? [英] How can i run php script with timer?

查看:140
本文介绍了我怎样才能运行PHP脚本与计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ names = array(Alex,Brad,Tom ); 
foreach($ names as $ name){
echo$ name< br />;
sleep(3);



$ b我怎样才能打印每个名字,每3秒?
echo $ name我需要一些计时器来停止循环,并等待3秒钟。
sleep()函数在这里不起作用。还有什么我可以做的?

解决方案

你不能做这个服务器端(可靠)。这是因为你的流量被占用或缓冲的可能性太大:通过网络服务器,代理服务器,浏览器等。然而,它可能在CLI上工作。



所以我肯定会在客户端执行,而不会牺牲安全性(如果需要的话)。

如果这仅仅是整型目的,你可以创建一个AJAX方法,它将返回名称数组,然后使用Javascript将其输出与定时器。或者,您可以让AJAX使用定时器调用PHP脚本,而PHP将只返回一个名称。它可以将当前的时间戳保存在数据库或Session变量中,并且在一段时间之后才能返回新的名称。


I have foreach functions that print students names

$names = array("Alex","Brad","Tom");
foreach($names as $name) {
   echo "$name <br />";
   sleep(3);
}

How can i print each name, each 3 seconds? After "echo $name" i need some timer to stop the loop and wait for 3 seconds. sleep() functions doesn't work here. What else i can do?

解决方案

You can not do this server side (reliably). This is because there is just too much possibility of your traffic being held or buffered on the way: by the webserver, by proxies along the way, by your browser, etc. It would however probably work on CLI.

So I would definitely do it client-side, without sacrificing security (if needed).

If this is only for "cosmetic" purposes, you can create an AJAX method which will return the array of names, and then have Javascript output them with its timers.

Or, you could have AJAX call the PHP script using timers, and PHP will return only one name. It can save the current timestamp in the DB or in a Session variable, and don't return a new name until some time has passed.

这篇关于我怎样才能运行PHP脚本与计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆