如何使用 php 在固定间隔内重复调用函数
How to call a function repeatedly in a fixed interval using php
我有一个用php编写的函数insertdata()。它从一个数据库表中获取值,然后将其插入到另一个数据库表中。
我想以固定的时间间隔重复此操作,直到单击停止按钮。请帮助我..
我尝试使用setInterval,但它只工作一次。我也用过
if(isset($_POST['stopbutton']))
$stop=1;
else
$stop=0;
function insertdata(){
while ($stop=0) {
//get the values from Table1 and insert it into Table2
}
sleep(1);
if ($stop==1)
echo stop;
}
请帮助我..
试试这段代码。它正在工作。
编辑的代码
重复插入.php
<?php
if(isset($_POST['stopbutton']))
{
echo "Stopped";
exit;
}
if(isset($_POST['startbutton']))
{
insertdata();
exit;
}
function insertdata(){
//Insert code here
}
?>
<html>
<head>
<title>Repeated Insert</title>
</head>
<body>
<input type="button" value="Start Insert" name="startbutton" onclick="start();" />
<input type="button" value="Stop Insert" name="stopbutton" onclick="stopInsert();" />
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
var intervals;
$(document).ready(function(){
});
function start()
{
startInsert();
intervals = setInterval(startInsert, 1000);
}
function startInsert() {
$.ajax({
type: "POST",
url: "repeatInsert.php",
data: {startbutton : true},
success: function(){
},
dataType: "json"
});
}
function stopInsert() {
clearInterval(intervals);
$.ajax({
type: "POST",
url: "repeatInsert.php",
data: {stopbutton : true},
success: function(){
},
dataType: "json"
});
}
</script>
</body>
</html>
您需要
继续检查$stop
变量。 提交帖子后,您的应用程序将不知道,因为 if 语句已被调用。 您可以通过在 while 循环中检查它来解决此问题。
这
基本上是你这样做时会发生什么:
CLIENT | SERVER
|
+--------+
| | stop = 0
| *----|--------> +------+
| | | loop |
| | | |
| | | |
| | | |
| | | | stop = 0
| *----|---------------------------> +------+
| | | | | loop |
| | | | | |
| | | | | | stop = 1
| *----|---------------------------------------------> +------+
| | | | | | | stop |
+--------+ | | | | +------+
+------+ | |
| |
| |
+------+
在服务器端脚本的每次调用中,$stop
的值永远不会更改。
您要做的是在服务器端的每次运行中只执行一个移动操作,并让客户端每隔一秒间隔执行一个请求。
您的任务是跟踪您在移动操作中的位置,并将其与客户端同步。
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量