首先我想说我在 Javascript/jQuery 方面的经验为零,而且我知道我的问题的解决方案实际上可能非常简单。
我想用我的 ESP8266 制作一个 WiFi 扫描仪,在页面上的表格中显示 SSID。到目前为止,下面的代码可以工作,但是延迟非常混乱。我认为如果 json 文件实际已填充,则必须可以在 while 循环中检查,如果是,则读取它并显示 ssid。
var foot = '<tr style="height:63px;"><td>Selected WiFi: ... Password: <input id="pw" type="text"><button onClick="connect()" class="btn btn-primary" type="button" style="margin-left:5%;">Connect</button></td></tr>';
var foot_last = '<tr style="height:63px;"><td>Selected WiFi: ... Password: <input id="pw" type="text"><button onClick="connect()" class="btn btn-primary" type="button" style="margin-left:5%;">Connect</button></td></tr>';
//gets called when the scan button is pressed
function scan() {
//Tells the ESP8266 to write the SSIDs in the json file scan.json (takes a while until its done)
$.post("/scan");
//this delay needs to be replaced. The code should only move on if the scan.json is not empty
delay(2000);
//Json gets read and contents displayed
$.getJSON("/scan.json", function(data) {
$.each(data, function(index, value) {
console.log(value);
var tbl_hoe = '<tr onClick="select()" style="height:63px;"><td>' + value + '</td></tr>';
foot = tbl_hoe + foot;
document.getElementById('tab').innerHTML = foot;
});
});
//at this point the json must be emtyed
}
function delay(ms) {
ms += new Date().getTime();
while (new Date() < ms) {}
}