0

I am developing a desktop app which will store data offline but whenever the desktop gets a connection a php script should detect it (cron job) and upload it to the online server like this:

while(true){
    if(connected == true){
        // run code;
    else{
        // wait to get connection
    }
}
Horai Nuri
  • 5,358
  • 16
  • 75
  • 127
  • and should not stop at all it should up and running from boot to shutting down the pc Please Help This is eating my head inside out – Zaki Muhammad Mulla Sep 05 '19 at 09:38
  • Or can i run javascript instead of php somehow – Zaki Muhammad Mulla Sep 05 '19 at 09:39
  • 1
    Add a cron job that runs every couple of minutes to run your pseudo code. – Dave Sep 05 '19 at 10:56
  • 1
    Can you provide some sample of what you've tried and where you're stuck? As it stands it looks like you're asking someone to write this app for you. – greggyb Sep 05 '19 at 21:37
  • sorry greggyb im not asking someone to write for me ive written a code which would do that but only explicitly , i wanted it to be automated . you know for instance when we connect to wifi your phone would update apps automatically. The difference is that i dont want to update anything , just going to upload to cloud whenever the app gets internet weather its wifi , lan or mobile hotspot and raoul here gave me the answer (which is navigator.onLine). – Zaki Muhammad Mulla Sep 06 '19 at 14:31

1 Answers1

1

Hi Zaki Muhammad Mulla,

I did some testing myself, and I came up with the following piece of code
Running the code snippets here won't work because this is a sandbox and there is no access to the localstorage here.

Make sure to include Jquery in your code

<script  
src="https://code.jquery.com/jquery-3.4.1.js" 
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous">
</script>

Then the actual function that will do the trick:

function processData(online){
        if(online == true){
            //My connection is online so I can execute code here
                    //If data is stored locally, read it and post it with an $.ajax POST
                    //I can loop through all the data and insert it per found row of data
                        for(var i=0, len=localStorage.length; i<len; i++) {
                            var key = localStorage.key(i);
                            var value = localStorage[key];
                                $.ajax({  
                                type: 'POST',  
                                url: 'PATH/TO/SCRIPT.php', 
                                data: { column: key, value: value },
                                success: function(response) {
                                    //The insert was succesful
                                    content.html(response);
                                }
                               });
                        }
                            
        }else{
            //Create your own loop here and fill data accordingly
            for(i = 0; i < 12; i++){
                localStorage.setItem("lastname" + i, "Smith");
                localStorage.setItem("firstname" + i, "John");
                localStorage.setItem("age" + i, "22");
                localStorage.setItem("job" + i, "Developer");    
            }
        }
    }

And at last the window.setInterval() to run a function every x seconds (Keep in mind 1 second = 1000 in the settimeout)

<script>
window.setInterval(function(){
        var online = navigator.onLine;
        processData(online);
    }, 1000);
</script>

Hope this may help you on your quest!
The sources I used:

  • Thanks a lot Sir really appreciate the help For Me the most helpfull part is navigator.onLine i was trying find this but i was failing to search correct keywords and because of wrong searches – Zaki Muhammad Mulla Sep 06 '19 at 14:33