Call php function from javascript every x minutes

I have a table that should be truncated every 12 hours. I created a header with a javascript function that display the current time for the operator and it refreshed every second. I tried to call a php function from that timer on a specific time that will truncate a table. But the problem is, when the application started it call that function immediately and it truncate the table.

script>
window.onload = function(){
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();

if (h==6 && m==0 && s=1)
{
var phpret="<?php trunctTable() ?>;";
}

  document.getElementById('my_time').innerHTML = today.toLocaleString();
  var t = setTimeout(startTime, 1000);

}


startTime();

}

/script>
< ? php
function trunctTable() {
sc_exec_sql(“truncate table mytable”);
}
?>

si trabajas sobre un registro de la base de datos guardando la fecha y hora del inicio y validas en tu función la condición.

I do not get the real situation here. I guess, you want to have a job that run every 12 hours, and the job is truncate a table.
You could use scheduler such as crontab in Linux. If you want to run a php, you could use like lynx (command line browser). So every 12 hours, the scheduler will run lynx to access a php that will do the truncate.
Or you could do it by scheduler on your database server.

1 Like