如何使用 php 在固定间隔内重复调用函数

How to call a function repeatedly in a fixed interval using php

本文关键字:调用 函数 php 何使用      更新时间:2023-09-26

我有一个用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的值永远不会更改。

您要做的是在服务器端的每次运行中只执行一个移动操作,并让客户端每隔一秒间隔执行一个请求。

您的任务是跟踪您在移动操作中的位置,并将其与客户端同步。