简体   繁体   English

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

[英]How to call a function repeatedly in a fixed interval using php

I have a function insertdata() which is written in php. 我有一个用PHP编写的函数insertdata()。 It is taking the values from one table of database and then inserting it into another table of database. 它从数据库的一个表中获取值,然后将其插入数据库的另一个表中。

I want to repeat this with a fixed time interval till stop button is clicked. 我想以固定的时间间隔重复此操作,直到单击“停止”按钮为止。 Please help me .. 请帮我 ..

I tried using setInterval, but its working only once. 我尝试使用setInterval,但是它只能工作一次。 Also I used 我也用过

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;
    }

Please help me .. 请帮我 ..

You need to keep checking the $stop variable. 您需要继续检查$stop变量。 After the post is submitted your application wont know because the if statement has already been called. 帖子提交后,您的应用程序将不知道,因为已经调用了if语句。 You can fix this by checking it in your while loop. 您可以通过在while循环中检查它来解决此问题。

This is basically what would happen if you're doing that: 如果这样做,基本上会发生以下情况:

CLIENT              |    SERVER
                    |
+--------+
|        | stop = 0
|   *----|--------> +------+
|        |          | loop |
|        |          |      |
|        |          |      |
|        |          |      |
|        |          |      | stop = 0
|   *----|---------------------------> +------+
|        |          |      |           | loop |
|        |          |      |           |      |
|        |          |      |           |      | stop = 1
|   *----|---------------------------------------------> +------+
|        |          |      |           |      |          | stop |
+--------+          |      |           |      |          +------+
                    +------+           |      |
                                       |      |
                                       |      |
                                       +------+

Within each invocation of the server side script, the value of $stop never changes. 在服务器端脚本的每次调用中, $stop的值不会改变。

What you will want to do is only perform one move operation in each run on the server side, and let the client side perform one request per one second interval. 您要做的就是在服务器端的每次运行中仅执行一次移动操作,并让客户端每隔一秒执行一次请求。

It's your task to keep track of where you are in the move operation and synchronise that with the client side. 跟踪您在移动操作中的位置并与客户端进行同步是您的任务。

Try this code. 试试这个代码。 It is working. 这是工作。

Edited Code 编辑代码

repeatInsert.php repeatInsert.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>

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

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