mercredi 26 juillet 2017

Refresh webpage on change in folder

I want to refresh my web page every time a file is uploaded to the folder.

I have a web service written in flask having the following handler

@app.route('/getlatest/')
def getlatest():
    import os
    import glob
    newset = max(glob.iglob('static/*'),key=os.path.getctime)
    Return newest;

This gives me the name of the latest file in the folder.

I have an Ajax call in my JS (client side) to constantly get data from above function.

function GetLatest()
            {
                $.ajax({
                    url: "http://localhost:5000/getlatest", 
                    success: function(result)
                    {
                        if(previousName != result){
                            previousName = result;
                            $("#image").attr("src","/"+previousName);
                        }
                    }
                });
            }

function calling server every second.

(function myLoop (i) {          
    setTimeout(function () {   
    GetLatest();                         
    if (--i) myLoop(i);
       }, 1000)
})(100);

This Works fine [well almost]. My Question is: Is there any better way to do it[ there must be ]?

i'm open to technology choices what every they may be [node, angualr etc.]




Aucun commentaire:

Enregistrer un commentaire