-1

首先我想说我在 Javascript/jQuery 方面的经验为零,而且我知道我的问题的解决方案实际上可能非常简单。

我想用我的 ESP8266 制作一个 WiFi 扫描仪,在页面上的表格中显示 SSID。到目前为止,下面的代码可以工作,但是延迟非常混乱。我认为如果 json 文件实际已填充,则必须可以在 while 循环中检查,如果是,则读取它并显示 ssid。

var foot = '<tr style="height:63px;"><td>Selected WiFi: ... &nbsp;Password:&nbsp;<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: ... &nbsp;Password:&nbsp;<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) {}
}
4

2 回答 2

0

戴夫牛顿的大坦克。我没有想到我可以使用 ESP8266 直接通过 http 发送数据,而中间没有那个 json

于 2018-07-08T13:08:05.513 回答
0

关于//this delay needs to be replaced. The code should only move on if the scan.json is not empty
如果您查看$.post的文档,您会发现它需要 2 个参数。第一个是 url,第二个是请求完成后调用的回调。

function scan() {
  $.post("/scan", function(data) {
    $.getJSON("/scan.json", function(data) {
      $.each(data, function(index, value) {
        console.log(value);
      });
    });
  });
}

关于://at this point the json must be emtyed
除非您向服务器发出请求,否则您无法从浏览器更改 json 文件的内容。(而且服务器一定已经实现了这样的功能)

于 2018-07-08T13:03:24.397 回答