2famous.TV
2famous.TV

Reputation: 470

Refresh Image onload with Javascript?

I'm working on a wordpress editor where I need to be able to refresh images without having to reload the page. However, I'm not getting the plot as I'm not really a javascript master :(

I'm running this, but in the console log, all I get is: "Uncaught ReferenceError: updateImage is not defined". What am I doing wrong?

<script>

    var newImage = new Image();
    newImage.src = "http://2famous.tv/wp-content/uploads/2013/07/Dubai-Sky-scrapers_thumb_one.jpg";

    function updateImage() {
    if(newImage.complete) {
        document.getElementById("thumb1").src = newImage.src;
        newImage = new Image();
        newImage.src = "http://2famous.tv/wp-content/uploads/2013/07/Dubai-Sky-scrapers_thumb_one.jpg?" + new Date().getTime();

    }

        setTimeout(updateImage, 1000);
    };


</script>

<img id="thumb1" src="image.jpg" onload="updateImage();">

Upvotes: 1

Views: 1623

Answers (1)

eKek0
eKek0

Reputation: 23289

When the <img> is loading, the <script> is not loaded yet. So, the updateImage() method doesn't exists. Try to use jquery, there you could do it easily with the ready event.

Upvotes: 1

Related Questions