douyan1896 2015-02-11 09:44
浏览 22

使用settimeout和json_encode更新javascript数组

I have an PHP file that automatically search a directory for png files. This file is called update.php. This file contains an array ($files) with this images.

Now I get this array in javascript with json_encode, and this is working fine. Every minute there is a new image in my directory. But when I am using autorefresh with settimeout, then the array not updating.

Script:

function vulArray() {
    for (k=0; k<dataArray.length; k++) {
        imagesArray[k].setMap(null);
    }

    // data arrays leeg gooien
    dataArray.splice(0,dataArray.length); // data array leeggooien
    imagesArray.splice(0,imagesArray.length); // oude array leegmaken

    dataArray = <?php echo json_encode($files); ?>; // php array omzetten naar javascript array

    var overlayOpts = {opacity:0.0} // Standaard dekking op 0 zetten

    // data array vullen met nieuwe data
    for (k=0; k<dataArray.length; k++) { // loop starten
        var url = dataArray[k]; // images uit de dataarray toewijzen
        image = new google.maps.GroundOverlay(url, borders, overlayOpts); // nieuwe laag aanmaken
        image.setMap(map); // imagelaag op de map zetten
        imagesArray.push(image); // imagelaag in de array pushen
    }

    refreshImage = setTimeout(function() {
        DownloadUrl("update.php", vulArray);
    }, 15000); // 5 minuten = 300000
}

The DownloadUrl function:

function DownloadUrl(url, callback) {
    var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;

    request.onreadystatechange = function() {
        if(request.readyState == 4) {
            request.onreadystatechange = doNothing;
            callback(request, request.status);
        }
    };

    request.open('GET', url, true);
    request.send(null);
    }

    function doNothing() {}

The file is executed every 15 seconds, this is working fine, but nothing will be updates for some sort of reason. Im emptying the arrays in the beginning of the function, but it still outputs the same array.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度