我有一个运行计数器的JavaScript。但是,我有一个php代码来检查我的日期数据库,无论我的开关状态是打开还是关闭。如果启用,我的计数器将继续计数。否则,计数器停止。
如何在onload函数中创建onload函数?
假设我有一个onload函数,它将在开关打开时计算运行时间。该函数中的一个onload也将执行clearInterval或clearTimeout。
function test() {
var rowres = "<?php echo $rowres; ?>";
var prodprep = "<?php
$sql = "SELECT energyCon FROM wcr WHERE id=1;";
$res = mysqli_query($db,$sql);
$row = mysqli_fetch_array($res);
$fnRes = $row['energyCon'];
echo $fnRes;
?>";
console.log(prodprep);
var newTime = new Date();
var curr_year = newTime.getFullYear();
var curr_Month = newTime.getMonth();
var curr_date = newTime.getDate();
var todayDate = (curr_year + " " + (curr_Month + 1) + " " + curr_date);
var prodprep1 = prodprep/3600;
newTime.setHours(newTime.getHours());
newTime.setMinutes(newTime.getMinutes());
newTime.setSeconds(newTime.getSeconds());
var newTime = newTime.getHours() + ":" + newTime.getMinutes() + ":" + newTime.getSeconds();
var timeDateNow = todayDate + " " + newTime;
// alert(timeDateNow);
upTime(rowres);
function upTime(countTo) {
now = new Date();
countTo = new Date(countTo);
difference = (now-countTo);
res=Math.floor(difference/1000);
var constant = res;
var prod = res*prodprep1;
document.getElementById('seconds').firstChild.nodeValue = prod;
$.post('updateProd.php',
{
'updatingProd' : true,
'prod' : prod
}, function(data,status)
{
});
clearTimeout(upTime.to);
upTime.to=setTimeout(function(){ upTime(countTo); },1000);
}
// });
}
function stopcounter() {
clearInterval(upTime.to);
alert("working");
}