关于控制时间更新的一个操作,以下代码中,上面6个参数都是通过$_SESSION获取,并且已经能够获取到$id数据,但是在点击“开始”的时候,action始终无法传递数据到这个取实现$action === 'start'的后续操作,请帮我看一下:
<?php
require_once '../../connections/games.php';
date_default_timezone_set('Asia/Shanghai');
// 获取操作命令
$action = isset($_POST['action']) ? $_POST['action'] : null;
// 初始化一个PDO错误处理
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("SET time_zone = '+8:00'");
$orderstmt = $pdo->prepare("SELECT * FROM m_dk_date WHERE `name` = ? AND `game` = ? AND `station` = ? AND `date` = ? AND `apm` = ? AND `order` = ?");
// 绑定值到占位符
$orderstmt->bindParam(1, $m_name, PDO::PARAM_STR);
$orderstmt->bindParam(2, $game, PDO::PARAM_STR);
$orderstmt->bindParam(3, $station, PDO::PARAM_STR);
$orderstmt->bindParam(4, $date, PDO::PARAM_STR);
$orderstmt->bindParam(5, $apm, PDO::PARAM_STR);
$orderstmt->bindParam(6, $order, PDO::PARAM_STR);
$orderstmt->execute();
$orders = $orderstmt->fetch(PDO::FETCH_ASSOC);
$id = $orders['id'];
$selectStmt = $pdo->prepare("SELECT * FROM m_dk_date WHERE id = ?");
$selectStmt->bindParam(1, $id, PDO::PARAM_INT);
$selectStmt->execute();
$row = $selectStmt->fetch(PDO::FETCH_ASSOC);
echo "这里是".$id.$action;
$total_time = '';
$new_start_time = '';
// 处理不同的操作
if ($action === 'start') {
$total_time = isset($_POST['total_time']) ? (int)$_POST['total_time'] : 0;
$updateStmt = $pdo->prepare("UPDATE m_dk_date SET total_time = ?, start_time = NOW(), status = 'running' WHERE id = ?");
$updateStmt->execute([$total_time, $id]);
} elseif ($action === 'pause') {
$updateStmt = $pdo->prepare("UPDATE m_dk_date SET paused_time = NOW(), status = 'paused' WHERE id = ?");
$updateStmt->execute([$id]);
}elseif ($action === 'continue') {
if ($row['status'] === 'paused') {
$paused_time = strtotime($row['paused_time']);
$start_time = strtotime($row['start_time']);
$elapsed = $paused_time - $start_time;
$total_time = $row['total_time']; // 假设这是倒计时总时长
$remaining_time = $total_time - $elapsed;
$new_start_time = date("Y-m-d H:i:s", time() - $elapsed); // 计算新的开始时间
$updateStmt = $pdo->prepare("UPDATE m_dk_date SET start_time = ?, status = 'running' WHERE id = ?");
$updateStmt->execute([$new_start_time, $id]);
}
} elseif ($action === 'stop') {
$updateStmt = $pdo->prepare("UPDATE m_dk_date SET status = 'stopped' WHERE id = ?");
$updateStmt->bindParam([$id]);
}
echo $action.$total_time.$new_start_time;
?>
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
@font-face {
font-family: '微软雅旗黑'; /* 定义字体的名称 */
src: url('../..fonts/MICROSOFTYAQIHEIBOLD-2.ttf') format('tff'); /* 指定woff2格式的字体文件路径和格式 */
font-weight: normal; /* 定义字体的粗细 */
font-style: normal; /* 定义字体的样式,如斜体等 */
}
.time_view {
font-family: '微软雅旗黑', sans-serif;
font-size:40px;
text-align:center;
}
.button-time-control {
background:#8eb832;
width: 54px;
height: 30px;
font-size:16px;
padding:5px;
}
</style>
<head>
<title>倒计时_控制端</title>
<script>
let countdownInterval = null; // 用于存储setTimeout的引用,以便可以清除它
function control(action) {
const formData = new FormData();
formData.append('action', action);
if (action === 'start') {
const totalTime = prompt('请输入倒计时总时长(秒):');
if (totalTime && !isNaN(totalTime)) {
formData.append('total_time', totalTime);
} else {
alert('请输入有效的数字!');
return;
}
}
fetch('control.php', {
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
// 操作完成后更新倒计时状态
startOrUpdateCountdown();
})
.catch(error => {
console.error('Error controlling countdown:', error);
alert('无法控制倒计时,请稍后重试!');
});
}
function startOrUpdateCountdown() {
if (countdownInterval) {
// 如果已经有一个倒计时在运行,先清除它
clearTimeout(countdownInterval);
countdownInterval = null;
}
updateCountdown();
// 注意:这里我们使用setTimeout而不是setInterval,以便在倒计时结束时能够停止它
countdownInterval = setTimeout(updateCountdown, 1000);
}
function updateCountdown() {
fetch('get_status.php')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data.status === 'running') {
const remainingTime = parseInt(data.remaining_time, 10); // 确保转换为整数
const minutes = Math.floor(remainingTime / 60);
const seconds = remainingTime % 60;
document.getElementById('countdown').innerText = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
if (remainingTime > 0) {
// 递归调用自己,但使用setTimeout来避免使用setInterval
countdownInterval = setTimeout(updateCountdown, 1000);
} else {
// 倒计时结束,清除间隔
clearTimeout(countdownInterval);
countdownInterval = null;
document.getElementById('countdown').innerText = '00:00';
}
} else if (isPaused) {
// 倒计时不是运行状态,清除间隔(如果它存在)
if (pausedTime !== null) {
const minutes = Math.floor(pausedTime / 60);
const seconds = pausedTime % 60;
document.getElementById('countdown').innerText = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}(暂停)`;
}
} else {
// 倒计时不是运行状态或未暂停,清除间隔(如果它存在)
if (countdownInterval) {
clearTimeout(countdownInterval);
countdownInterval = null;
}
document.getElementById('countdown').innerText = '倒计时已停止';
}
})
// 假设你有一个按钮用于暂停倒计时
function pauseCountdown() {
if (countdownInterval && !isPaused) {
clearTimeout(countdownInterval);
countdownInterval = null;
// 保存当前剩余时间
pausedTime = parseInt(data.remaining_time, 10);
isPaused = true;
updateCountdown(); // 更新显示以反映暂停状态
}
}
// 假设你还有一个按钮用于恢复倒计时(如果需要的话)
function resumeCountdown() {
if (isPaused) {
isPaused = false;
updateCountdown(); // 这将重新开始倒计时
}
}
function stopCountdown() {
if (countdownInterval) {
clearTimeout(countdownInterval);
countdownInterval = null;
}
document.getElementById('countdown').innerText = '10:00';
remainingTime = null; // 重置剩余时间
isPaused = false; // 重置暂停状态(虽然在这个场景中可能不是必需的,但保持一致性是个好习惯)
}
}
</script>
</head>
<body>
<div class="time_view" id="countdown">加载中...</div>
<div style="text-align:center;background:#1f1f1f;padding:5px;height:40px;width:260px;margin:auto;">
<button class="button-time-control" onclick="control('start')">开始</button>
<button class="button-time-control" onclick="control('pause')">暂停</button>
<button class="button-time-control" onclick="control('continue')">继续</button>
<button class="button-time-control" onclick="control('stop')">停止</button>
</div>
</body>
</html>