运行 Php 脚本不断,如果作为 windows 服务连接到互联网,它将数据插入 xampp 服务器
Run Php script constantly and if connected to internet as windows service which inserts data into xampp server
我正在开发一个桌面应用程序,它将离线存储数据,但只要桌面获得连接,php 脚本就会检测到它(cron 作业)并将其上传到在线服务器,如下所示:
while(true){
if(connected == true){
// run code;
else{
// wait to get connection
}
}
嗨,扎基·穆罕默德·穆拉,
我自己做了一些测试,得出了下面这段代码
运行 此处的代码片段将不起作用,因为这是一个沙箱,无法访问此处的本地存储。
确保在您的代码中包含 Jquery
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous">
</script>
那么实际的功能就是:
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");
}
}
}
最后 window.setInterval() 到 运行 每 x 秒一个函数(记住 settimeout 中的 1 秒 = 1000)
<script>
window.setInterval(function(){
var online = navigator.onLine;
processData(online);
}, 1000);
</script>
希望这对您的探索有所帮助!
我使用的来源:
- https://medium.com/@Carmichaelize/checking-for-an-online-connection-with-javascript-5de1fdeac336
- Ajax passing data to php script
- https://www.w3schools.com/html/html5_webstorage.asp
- https://www.taniarascia.com/how-to-use-local-storage-with-javascript/
- Get HTML5 localStorage keys
- HTML5 localStorage getting key from value
- What's the easiest way to call a function every 5 seconds in jQuery?
我正在开发一个桌面应用程序,它将离线存储数据,但只要桌面获得连接,php 脚本就会检测到它(cron 作业)并将其上传到在线服务器,如下所示:
while(true){
if(connected == true){
// run code;
else{
// wait to get connection
}
}
嗨,扎基·穆罕默德·穆拉,
我自己做了一些测试,得出了下面这段代码
运行 此处的代码片段将不起作用,因为这是一个沙箱,无法访问此处的本地存储。
确保在您的代码中包含 Jquery
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous">
</script>
那么实际的功能就是:
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");
}
}
}
最后 window.setInterval() 到 运行 每 x 秒一个函数(记住 settimeout 中的 1 秒 = 1000)
<script>
window.setInterval(function(){
var online = navigator.onLine;
processData(online);
}, 1000);
</script>
希望这对您的探索有所帮助!
我使用的来源:
- https://medium.com/@Carmichaelize/checking-for-an-online-connection-with-javascript-5de1fdeac336
- Ajax passing data to php script
- https://www.w3schools.com/html/html5_webstorage.asp
- https://www.taniarascia.com/how-to-use-local-storage-with-javascript/
- Get HTML5 localStorage keys
- HTML5 localStorage getting key from value
- What's the easiest way to call a function every 5 seconds in jQuery?