利用异步Json请求/方法调用更快的页面加载

时间:2017-06-29 14:48:01

标签: javascript c# json asp.net-mvc

我正在研究由4盒信息组成的数字标牌,因为它目前正在向控制器发出1个单独的Json请求,以检索将在框中显示的数据。 action方法在数据库中查询信息并调用4个单独的私有方法,提供查询信息以填充要作为json返回到视图的列表。

以下提供的示例代码。

我的问题是,如何异步完成,或者在控制器端进行多线程处理,或者对4个单独的操作方法进行4次单独的Json请求。

当前控制器更改方法:

public ActionResult Change()
{

  Dictionary<string, int> model = new Dictionary<string, int>();

  var q1 = db.getData1();
  var q2 = db.getData2();
  var q3 = db.getData3();
  var q4 = db.getData4();


  var content = GetEfficiencyValues(q1, q2, q3);
  model.Add("currEff", content[0]);
  model.Add("prevEff", content[1]);
  model.Add("otherEff", content[2]);

  content = GetProductivityValues(q1, q2, q4);
  model.Add("currProd", content[0]);
  model.Add("prevProd", content[1]);
  model.Add("otherProd", content[2]);

  Content = GetSlowStartValues(q1, q2, q3, q4);
  model.Add("currAvg", content[0]);
  model.Add("prevAvg", content[1]);
  model.Add("otherAvg", content[2]);

  //adding WIP content for the box
  content = GetNotSwipedInCount(q1, q2);
  model.Add("WIP", content[0]);
  model.Add("total", content[1]);

  return Json(model, JsonRequestBehavior.AllowGet);
}

&#13;
&#13;
$(document).ready(function() {
  getData();
});

var dataList = [{}];

function getData() {

  dataList = [{}];
  var getData = '@Url.Action("Change")';

  //get json from controller (DigitalSignage/Change)
  $.getJSON(getData, function(data) {
    dataList = data;

    changeData();
  });
}

function changeData() {

  //refreshing efficiency data
  createGauge(dataList.currEff, "#g1")
  $("#eff1").text(dataList.currEff + "%");
  $("#eff2").text(dataList.prevEff + "%");
  $("#eff3").text(dataList.otherEff + "%");

  //refreshing the production data
  createGauge(dataList.currProd, "#g2")
  $("#prod1").text(dataList.currProd + "%");
  $("#prod2").text(dataList.prevProd + "%");
  $("#prod3").text(dataList.otherProd + "%");

  //refreshing the average data
  $("#avg1").text(dataList.currAvg + " mins");
  $("#avg2").text(dataList.prevAvg + " mins");
  $("#avg3").text(dataList.otherAvg + " mins");

  //refreshing the content of the WIP box
  $("#wip").text(dataList.WIP);
  $("#attend").text("of " + dataList.total);


  //loop back in 5 minutes
  window.setInterval(getData, 300000);

}
&#13;
<style>body {
  background-color: #444444;
  display: none;
}

i {
  color: white;
}

.fa {
  font-size: 2.5em;
}

.info-section {
  background-color: #232323;
  border-radius: 16px;
  padding: 25px;
  margin: 5px auto;
  max-width: 560px;
  min-height: 430px;
}

.info-section h1,h2,h3,h4,h5,h6 {
  color: white;
  text-align: center;
  margin: 0px;
}

.info-section > div {
  border: 1px solid white;
}

.legend {
  display: inline-block;
  border-radius: 3px;
  border: 0px;
  margin: 0px 2px;
  font-weight: bold;
  font-size: 1em;
}

.result-text {
  text-align: center;
  font-size: 45px;
  padding: 15px 0px;
  margin: 0px;
  color: white;
}

.gauge-val {
  color: white;
  font-size: 40px;
  position: relative;
  left: 100px;
  bottom: 45px;
}

.gauge {
  margin: auto;
}

#g1,
#g2,
.gauge-container {
  width: 180px;
  margin: auto;
}

</style>
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<head>
  <link rel="stylesheet" href="~/Content/font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="row">
  <p id="time-stamp" style="float:right; color:white; padding: 15px 30px 0;">Last Updated:</p>
</div>
<div class="row">
  <!-- Efficiency Stats Block-->
  <div class="col-sm-6">
    <div class="info-section">

      <h1>Efficiency</h1>

      <div class="row" style="border: 1px solid white; margin-left: 0px; margin-right: 0px;">

        <div class="col-sm-6 today" style="border-right: 1px solid white; padding:0px;">
          <h3>Current</h3>
          <div class="gauge-container">
            <div class="gauge" id="g1"></div>
            <div id="eff1" class="gauge-val percent"></div>
          </div>
          <i class="fa "></i>
        </div>

        <div class="col-sm-6 yesterday" style=" padding:0px;">
          <div style=" border-bottom: 1px solid white;">
            <h3>Yesterday</h3>
            <p id="eff2" class="result-text percent"></p>
            <i class="fa "></i>
          </div>
          <div>
            <h3>Yesterday</h3>
            <p id="eff3" class="result-text percent">84%</p>
            <i class="fa "></i>
          </div>
        </div>

      </div>
      <div style="border:0px; margin:auto; float:right; margin:5px;">
        <div class="legend" style="background-color:#ff3823; ">0% - 75%</div>
        <div class="legend" style="background-color:#f4eb49; ">75% - 82%</div>
        <div class="legend" style="background-color:#72bb53; ">83% - 100%</div>
      </div>
    </div>
  </div>
  <!-- Task Card productivity Block-->
  <div class="col-sm-6">
    <div class="info-section">
      <h1>Task Card Productivity</h1>
      <div class="row " style="border: 1px solid white;  margin-left: 0px; margin-right: 0px;">
        <div class="col-sm-6 today" style="border-right: 1px solid white; padding:0px;">
          <h3>Current</h3>
          <div class="gauge-container">
            <div class="gauge" id="g2"></div>
            <div id="prod1" class="gauge-val percent"></div>
          </div>
          <i class="fa "></i>
        </div>

        <div class="col-sm-6 yesterday" style=" padding:0px;">
          <div style=" border-bottom: 1px solid white;">
            <h3>Yesterday</h3>
            <p id="prod2" class="result-text percent"></p>
            <i class="fa "></i>
          </div>
          <div>
            <h3>Yesterday</h3>
            <p id="prod3" class="result-text percent"></p>
            <i class="fa "></i>
          </div>
        </div>
      </div>
      <div style="border:0px; float:right; margin:5px;">
        <div class="legend" style="background-color: #ff3823;">0% - 75%</div>
        <div class="legend" style="background-color: #f4eb49;">75% - 82%</div>
        <div class="legend" style="background-color: #72bb53;">83% - 100%</div>
      </div>
    </div>
  </div>
  <!-- Slow Start average Block-->
  <div class="col-sm-6">
    <div class="info-section">
      <h1>Slow Start Average</h1>
      <div class="row" style="border: 1px solid white;  margin-left: 0px; margin-right: 0px;">
        <div class="col-sm-6 today" style="padding:0px;">
          <h3>Current</h3>
          <p id="avg1" class="result-text minute" style=" font-size: 3.5em; padding:70px 0px;"></p>
          <i class="fa "></i>
        </div>
        <div class="col-sm-6 yesterday" style="border-left: 1px solid white; padding:0px;">
          <div style=" border-bottom: 1px solid white;">
            <h3>Yesterday</h3>
            <p id="avg2" class="result-text minute"></p>
            <i class="fa "></i>
          </div>
          <div>
            <h3>Yesterday</h3>
            <p id="avg3" class="result-text minute"></p>
            <i class="fa "></i>
          </div>
        </div>
      </div>
      <div style="border:0px; float:right; margin:5px;">
        <div class="legend" style="background-color: #ff3823;"> 0 - 15 minutes </div>
        <div class="legend" style="background-color: #72bb53;"> 15 - &infin; minutes </div>
      </div>
    </div>
  </div>
  <!-- Not Swiped into Trax Block-->
  <div class="col-sm-6">
    <div class="info-section">
      <h1>Not Swiped Into Trax</h1>
      <div class="row " style="border: 1px solid white;  margin-left: 0px; margin-right: 0px;">
        <h1>Current</h1>
        <p id="wip" class="result-text " style="font-size:155px;"> </p>
        <h1 id="attend">of 50</h1>
      </div>
    </div>
  </div>
</div>
</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这是你要找的吗?我编辑它以包含一个简单的查询缓存。

UITableView