我正在建立一个网站,我无法弄清楚一件事。我需要一个脚本来检查自加载时间以来是否已经过了10秒,然后它将运行另一个PHP脚本。但我不确定是否有可能。我已经尝试过这个问题。有任何想法吗?提前谢谢!
if (
<script type="text/javascript">
function viewplus(){
}
setTimeout(viewplus, 10000);
</script>
)
$query = "SELECT * FROM users WHERE id=" . $rws2['id_user'];
$result2 = mysqli_query($db, $query);
$rws2 = mysqli_fetch_array($result2);
$views_total = $rws2['views_total'] + 1;
$views_week = $rws2['views_week'] + 1;
$views_today = $rws2['views_today'] + 1;
$id = $rws2['id'];
$query = "UPDATE users SET views_total='$views_total',views_week='$views_week',views_today='$views_today' WHERE id='$id'";
mysqli_query($db, $query);
答案 0 :(得分:0)
可以像这样使用jQuery: 将您的代码放在php文件中,并在页面加载后10秒后用
调用它<script type="text/javascript">
// Check if the page has loaded completely
$(document).ready( function() {
setTimeout( function() {
$('#some_id').load('index.php');
}, 10000);
});
</script>
更新表并接收状态消息的示例:
if (isset($_POST['id'])&&
isset($_POST['var'])){
$con = new mysqli("localhost", "my_user", "my_password", "world");
$id = $con->real_escape_string($_POST['id1']); //In our example id is INT
$var = $con->real_escape_string($_POST['var']);
$result = $con->query("UPDATE table SET value = '$var' WHERE id = $id);
(!$result) ? echo "Update failed!" : "Update succeeded";
}
这会将php文件的输出(更新已归档或已成功)加载到元素中(此情况为id some_id
)。
我不推荐的另一种方法是使用PHP的sleep()
函数PHP Documentation