[英]Time based Code execution in Javascript PHP
I have a web application in PHP and Javascript where i want to apply a time based question generation. 我有一个使用PHP和Javascript的Web应用程序,我想在其中应用基于时间的问题生成。 I have preset of code that will be executed at a specific time which will generate the question.
我预先设置了将在特定时间执行的代码,这将生成问题。
Now i am thinking that how can i apply this. 现在我在想如何应用此功能。 Here are some the information regarding the web
以下是有关网络的一些信息
Now, i can make use SetTimeOut and Javascript Date object in order to acheive the desire functionality: Here 现在,我可以使用SetTimeOut和Javascript Date对象来实现所需的功能:
function checkVotingQuestionTime()
{
var currentTime = new Date();
if(currentTime.getHours() >= 16 && currentTime.getHours() < 23)//check the time from user OS
{
checkVoteQuestionGenerated();
}
}
function checkVoteQuestionGenerated(){
//query to php check question exists
//if no
//then generate question
}
And Register it at document ready event in main page(after login). 并在主页上的文档就绪事件中注册它(登录后)。 This code will be open for all users.
该代码将对所有用户开放。
$jq(document).ready(function() {
setInterval(checkVotingQuestionTime, 60000);
}
As it appear that, this is not feasible code, because 看来,这是不可行的代码,因为
So how can I control above limitations or is there any other proper solution available? 那么,如何控制上述限制?是否有其他合适的解决方案可用?
Having 100 clients poll the server every few minutes is not a big deal. 每隔几分钟让100个客户端轮询服务器并不是什么大问题。 Every time they open a page on your website that's in effect polling the server.
每次他们在您的网站上打开一个实际上轮询服务器的页面时。 So moving the date check to php on the server side is the best solution for what you have currently set up.
因此,将日期检查移到服务器端的php上是当前设置的最佳解决方案。
If you don't like the idea of having the client poll the server every now and then for aesthetic reasons (and I can appreciate that), for example you want the question to be refreshed at 16:00 on the dot, you could look into using a sockets. 如果您不喜欢让客户端出于美学原因不时地轮询服务器的想法(我可以理解),例如,您希望在16:00时刷新问题,您可以看一下使用插座。 This, in effect, means the client is listening to any changes sent from the server, without the need to poll it, and updates when necessary.
实际上,这意味着客户端正在监听从服务器发送的任何更改,而无需轮询它,并在必要时进行更新。
Socket.io is easy to use, but not the simplest when it comes to interfacing with php. Socket.io易于使用,但在与php交互时却不是最简单的。 I don't know if you're familiar with Node.js but here's a small discussion some others had regarding sockets and php:
我不知道您是否熟悉Node.js,但这是一些关于套接字和php的小讨论:
What is the best way to use websockets along with PHP and MySQL scripts? 将Websocket与PHP和MySQL脚本一起使用的最佳方法是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.