【问题标题】:Web page becomes unresponsive and crashes after few minutes网页在几分钟后变得无响应并崩溃
【发布时间】:2019-08-12 18:06:16
【问题描述】:

我的网页在托管在服务器上几分钟后变得无响应、冻结和崩溃,但在本地主机上运行时工作正常。 我正在从 Flask Server(app) 获取 api 数据

api 引擎在 Flask 上工作,每 1 秒以 JSON 形式提取数据。还根据从 api 获得的 Lat Lon 更新谷歌地图标记

我希望网页不会冻结或无响应

var map_lat = 28.644800;
var map_lon = 77.216721;
var head = 0;
var ngUrl = "http://*******.com/";


function httpGet(theUrl) {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET", theUrl, false); // false for synchronous request
  xmlHttp.send(null);
  return xmlHttp.responseText;
}

function buttonFunctionPR(id) {

  url = ngUrl.concat("x1/");
  var response = httpGet(url + id);
  response = JSON.parse(response);
  document.getElementById("message").innerHTML = response['msg'];
}



hcFunOne = async() => {

  url = ngUrl.concat("x5/");
  var response = await httpGet(url);
  response = JSON.parse(response);
  document.getElementById("alti").innerHTML = response['msg'];
  console.log(response['msg']);
  setTimeout(() => {
    hcFunOne();
  }, 1000)
}


hcFunTwo = async() => {

  url = ngUrl.concat("x6/");
  var response = await httpGet(url);
  response = JSON.parse(response);
  var dataLB = response['msg2']
  var dataLB = dataLB.split("/");
  var htmlrespL = "Lattitude : <b>" + response['msg1'] + "</b>"
  var htmlrespB = "<b>Battery</b><hr class='hru hrusm'>Vlts : " + dataLB[0] + "<br>Level : " + dataLB[2] + " %&nbsp; &nbsp;&nbsp; ";

  document.getElementById("lat").innerHTML = htmlrespL;
  document.getElementById("batt").innerHTML = htmlrespB;

  window['map_lat'] = parseFloat(response['msg1']);
  setTimeout(() => {
    hcFunTwo();
  }, 1000)
}


hcFunThree = async() => {

  url = ngUrl.concat("x7/");
  var response = await httpGet(url);
  response = JSON.parse(response);
  var htmlresp = "Longitude : <b>" + response['msg'] + "</b>"
  document.getElementById("lon").innerHTML = htmlresp;

  window['map_lon'] = parseFloat(response['msg']);
  setTimeout(() => {
    hcFunThree();
  }, 1000)
}

hcFunFour = async() => {

  url = ngUrl.concat("x8/");
  var response = await httpGet(url);
  response = JSON.parse(response);
  //var data = response.split("/");
  var htmlresp = "<b>System Usage</b><hr class='hru hrusm'>CPU : " + response['cpu'] + "%<br>RAM : " + response['ram'] + "%";

  document.getElementById("cpu").innerHTML = htmlresp;
  setTimeout(() => {
    hcFunFour();
  }, 1000)
}

hcFunFive = async() => {

  url = ngUrl.concat("x9/");
  var response = await httpGet(url);
  response = JSON.parse(response);
  window['head'] = parseFloat(response['msg']);
  //console.log(head);
  //console.log(typeof window['head']);
  setTimeout(() => {
    hcFunFive();
  }, 1000)

}


setTimeout(function() {
  hcFunOne();
  hcFunTwo();
  hcFunThree();
  hcFunFour();
  hcFunFive();
}, 500);

// ------MAP WORK-----


var map;
var markers = [];

function initMap() {
  var haightAshbury = {
    lat: window.map_lat,
    lng: window.map_lon
  };


  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 18,
    center: haightAshbury,
    mapTypeId: 'terrain'
  });



  setInterval(function() {


    var lats = window.map_lat;
    var lngs = window.map_lon;
    map.setCenter({
      lat: lats,
      lng: lngs
    });


    var temp2 = {
      lat: lats,
      lng: lngs


    }
    /
    addMarker(temp2);

    console.log(head);

  }, 2000);


  addMarker(haightAshbury);
}


function addMarker(location) {
  setMapOnAll(null);



  var marker = new google.maps.Marker({
    position: location,
    map: map,

    icon: {
      path: "M0 0 L0 25 L 25 0 L 0 0",
      fillColor: '#CC0000',
      fillOpacity: .8,
      anchor: new google.maps.Point(0, 0),
      strokeWeight: 1.5,
      scale: 1.3,
      rotation: 45 + head,


    }
  });
  markers.push(marker);
}


function setMapOnAll(map) {
  for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(map);
  }
}


initMap();

【问题讨论】:

  • 我看不到同步获取和异步代码的意义。异步使用 Fetch。还要干燥(不要重复自己),使用数组和循环。为什么间隔???
  • 这样它也应该在 localhost 中变得无响应,但它在 localhost 中工作正常
  • 浏览器限制了可以同时通过一个页面的请求数。添加网络延迟会延迟请求和响应,从而在每个请求添加到队列时阻塞页面的处理。最终,这将导致 UI 冻结,并可能导致 Javascript 引擎和/或浏览器在尝试处理混乱时停止正常运行。
  • @daddygames 如何解决这个问题?这将是一个很大的帮助
  • 如果你真的需要像这样与服务器进行近乎恒定的通信,那么 WebSocket 或类似的东西可能是推荐的解决方案。您可能会发现这篇文章很有帮助 -> blog.stanko.io/do-you-really-need-websockets-343aed40aa9b

标签: javascript ajax flask


【解决方案1】:

您每秒发出的 http 请求太多。我认为您应该考虑改用 WebSocket。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    相关资源
    最近更新 更多