How to change the field value after 'X' mi

2019-08-15 04:21发布

I want to update my value after 10 minutes of the first update

For example, I've sent this request:

UPDATE client set lockclient = 1 WHERE id = 100

I want to send another request after 10 minutes, as below:

UPDATE client set lockclient = 0 WHERE id = 100

I've found this example on google:

   DELIMITER $$
   CREATE EVENT deactivation
    ON SCHEDULE EVERY 10 MINUTE STARTS CURRENT_TIMESTAMP
    DO
      BEGIN
        UPDATE tbl SET tbl.active = FALSE WHERE id =10
      END;
  $$;

But I want to update only in the first 10 min, not for each 10 min. Can I do that with php, mysql or javascript ?

2条回答
仙女界的扛把子
2楼-- · 2019-08-15 05:03

You can try Ajax with setTimeout() javascript function like this:

<div id="id_test"></div>
<SCRIPT language=javascript>
  var update1 = false
      update2 = false;

  function run_ajax(lockclient){  
    // write your call ajax function with lockclient parameter

    // this is an example to test the principle
    var dt=new Date()

    document.getElementById("id_test").innerHTML = dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds() + "<br>lockclient =" + lockclient;   
  }    

  function Timer() {
    // if you have already called 2 times the server, you leave 
    if (update2 && update1)
      return;

    // if you have already called 1 time the server, you call it a second time with '0' as lockclient parameter 
    if (!update2 && update1){
      update2 = true;
      run_ajax(0)
    }        

    // If you did not call the server, you call it a first time with '1' as lockclient parameter 
    if (!update1){
      update1 = true;     
      run_ajax(1)
    }                 
    //setTimeout("Timer()",600000); -> 600000 = 10 minutes

    setTimeout("Timer()",2000); // 2s to test 

   }
   Timer();
</SCRIPT>
查看更多
Melony?
3楼-- · 2019-08-15 05:08

1st option:

There are couple of ways. If you want the short path you can accomplish by doing this by sql.

//Create an event called my_update_event
DELIMITER $$
DROP EVENT IF EXISTS my_update_event$$
CREATE EVENT my_update_event
    ON SCHEDULE
        AT NOW() + INTERVAL 10 MINUTE
    ON COMPLETION NOT PRESERVE
    ENABLE
DO BEGIN
    UPDATE tbl SET tbl.active = FALSE WHERE id =10;
END$$

2nd option:

I'm not sure what you mean by update just in the first 10 min? you can also use cron with php/mysql to do the update. You can setup a cron to do whatever, in this example run every 10 min

*/10 * * * * /path/to/script/update.php
查看更多
登录 后发表回答