0

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.

11
  • 1
    Can you provide DownloadUrl function code ? Commented Feb 11, 2015 at 9:48
  • 1
    dataArray = <?php echo json_encode($files); ?>; won't get evaluated... Commented Feb 11, 2015 at 10:47
  • 1
    PHP code will not be executed in this context. Commented Feb 11, 2015 at 10:50
  • 1
    It gets evaluated (read: executed) only once, when your PHP script that outputs this piece of JavaScript runs. After that, you have the result as a static value inside your JS code, that doesn’t change any more. Commented Feb 11, 2015 at 11:16
  • 1
    Request the new data from the server in JSON format, and then use that in your function – either by assigning it to a global variable first, or by passing it into it as a parameter … Commented Feb 11, 2015 at 12:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.