javascript - 如何刷新div而不刷新所有php数据

标签 javascript php jquery ajax refresh

我认为这很简单,但我一生都无法弄清楚。我想刷新一个 div 而不刷新所有内容。我在每个图像上都有一个计时器,从 24 小时倒计时到 0,然后消失。 .一切正常,但我似乎不能只刷新计时器 div..

我的 PHP -

$date = date('Y-m-d H:i:s');

$sql = "SELECT * FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    $path = $row['path'];
    $user = $row['user'];
    $update = $row['update_date'];

    $timeFirst  = strtotime($date);
    $timeSecond = strtotime($update);
    $timeSecond = $timeSecond + 86400;
    $timer = $timeSecond - $timeFirst;

if($timer <= 0){

}else{
    echo '<img id="pic" src="/v2/uploads/'.$path.'"/>';
    echo '<div id="user">#'.$user.'</div>';
    echo '<div id="timer">'.$timer.' </div>';

    }
  }
}

我想以 1 秒的间隔刷新计时器,而不是图像。我知道我可以使用 ajax 从外部文件调用它,据我所知,该文件加载所有内容。这仍然是新的。 *这不是示例的代码片段,并非全部。

最佳答案

根据我的评论,你可以这样做:

  1. 将“timer”类添加到您的 #timer 元素(如果您有多个 #timer 元素,请为每个元素使用不同的 ID)。
  2. 创建 php 脚本,每当调用时都会返回新的 $timer:

ajax-timer.php

<?php

/* include file where $conn is defined */


$response = array();

$date = date('Y-m-d H:i:s');

$sql = "SELECT * FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $update = $row['update_date'];

        $timeFirst  = strtotime($date);
        $timeSecond = strtotime($update);
        $timeSecond = $timeSecond + 86400;
        $timer = $timeSecond - $timeFirst;


        if($timer > 0) {
            //Add just timer to response array
            $response[] = $timer;
        }
    }
}

//Return json response and handle it later in ajax:
echo json_encode(array(
    'result'=>empty($response) ? 0 : 1,
    'data'=>$response));
die();
  • 使用 $.ajax 从 ajax-timer.php 请求数据,并在收到响应时填充数据:
  • timer-update.js

    var ajaxTimerThread = 0;
    var ajaxTimerRunning = false;
    
    function ajaxTimerRun() {
        //Prevent running function more than once at a time.
        if(ajaxTimerRunning)
            return;
    
        ajaxTimerRunning = true;
        $.post('ajax-timer.php', {}, function (response) {
            ajaxTimerRunning = false;
            try {
                //Try to parse JSON response
                response = $.parseJSON(response);
                if (response.result == 1) {
                    //We got timer data in response.data.
                    for(var i = 0; i < response.data.length; i++) {
                        var $timer = $('.timer').eq(i);
                        if($timer.length) {
                            $timer.html(response.data[i]);
                        }
                    }
                }
                else {
                    //Request was successful, but there's no timer data found.
                    //do nothing
                }
                //Run again
                ajaxTimerThread = setTimeout(ajaxTimerRun, 1000); //every second
            }
            catch (ex) {
                //Could not parse JSON? Something's wrong:
                console.log(ex);
            }
        });
    }
    
    $(document).ready(function() {
        // Start update on page load.
        ajaxTimerRun();
    })
    

    关于javascript - 如何刷新div而不刷新所有php数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39927991/

    相关文章:

    javascript - Redux 可观察到的 : dispatch action on multiple clicks (two or more)

    php, strtolower 和 mb_strtolower 有什么区别?

    javascript - 如何使用 jquery 动态切换样式表?

    php - 带有换行符示例的 Android SAX 解析器?

    php - PHP 是编译的还是解释的?

    javascript - 使用 jquery 为 div 分配样式

    javascript - 尝试访问 json 数据时未定义 (ajax)

    javascript - 如何使用css动画让元素一个一个出现

    javascript - Firefox 扩展将数据写入文件

    javascript - 如何避免 HTML 页面中的 CSS 干扰