1

我想问我如何制作一个 php 脚本,它在特定时间(例如 2 分钟后)后重复存储在数据库中的数据的 id。我不想使用 cron 作业或其他调度程序。只是 php或javascript实施。在此先感谢..

4

3 回答 3

3

我对这个脚本做了类似的事情。当用户在页面上时,它scriptToRun.php每 2 分钟运行一次。

function changeFeedAddress() {
    $.ajax({
        type: 'get',
            url: 'scriptToRun.php',
            success: function(txt) {
                // do something with the new RSS feed ID here
            }
        });
    }

setInterval(changeFeedAddress,120000); //   2 MINUTES
于 2011-01-12T00:41:28.663 回答
1

替代@JMC Creative(例如,自包含):

<?php
  // check if the $.post below is calling this script
  if (isset($_POST['ajax']))
  {
    // $data = /*Retrieve the id in the database*/;

    // ---vvvv---remove---vvvv---
    // Example Data for test purposes
    $data = rand(1,9999);
    // End Example Data
    // ---^^^^---remove---^^^^---

    // output the new ID to the page so the $.post can see it
    echo $data;
    exit; // and stop processing
  }
?>
<html>
  <head>
    <title>Demo Update</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(function(){
        // assign a timeout for this script
        var timeout = 2 * 60000; // 2 minutes

        // create a function we can call over and over to fetch the ID
        function updateDBValue(){
          // call this same script and retrieve an "id from the database" (see top of page)
          $.post('<?php echo $_SERVER['PHP_SELF']; ?>',{ajax:true},function(data){
            // 'data' now contains the new ID. For example's sake, place the value
            // in to an input field (as shown below)
            $('#db-value').val(data);

            // set a timer to re-call this function again
            setTimeout(updateDBValue,timeout);
          });
        }

        // call the function initially
        updateDBValue();
      });
    </script>
  </head>
  <body style="text-align:center;">
    <div style="margin: 0 auto;border:1px solid #000;display:block;width:150px;height:50px;">
      DB Value:<br />
      <input type="text" id="db-value" style="text-align:center;" />
    </div>
  </body>
</head>
于 2011-01-12T00:51:41.803 回答
1

为什么不这样做呢?

<?php
header('refresh: 600; url=http://www.yourhost.com/yourscript.php');

//script here
?>

如果您从脚本中随机生成您的 ID...这将正常工作。该页面将每 10 分钟刷新一次。

于 2011-01-12T02:21:35.713 回答