Angular JS API在100毫秒内刷新

时间:2017-01-03 19:29:07

标签: angularjs

我想每100毫秒刷新一次API,任何人都可以提供帮助。 代码从api获取数据并显示在表中。后端从不同的源更新,因此我们应该每100ms刷新一次数据。

<html>
<head>
<title>Angular JS Includes</title>
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f2f2f2;
}
table tr:nth-child(even) {
background-color: #ffffff;

}
</style>
</head>
<body>
<h2>AngularJS Cycle count application</h2>


<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
<tr>
<th>Item Number </th>
<th>UPC Number</th>
<th>Count Qnty</th>
</tr>

<tr ng-repeat="x in names">
<td>{{ x.ITEM }}</td>
<td>{{ x.UPC}}</td>
<td>{{ x.QNTY}}</td>

</tr>
</table>
</div>

<script    
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">                
</script>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {

$http.get("http://54.244.108.186:4000/api/cc_detail")
.then(function (response) {

$scope.names = response.data.item_upc;
});

});


</script>

</body>
</html>

1 个答案:

答案 0 :(得分:-3)

如果您想根据后端更新刷新屏幕,那么理想情况下您应该尝试使用HTML5 Server Sent Events,而不是每100毫秒点击一次特定的API。每隔100毫秒命中一次api会在服务器上创建一个新线程来处理请求,从而导致其他请求出现瓶颈......你会模拟DOS攻击。

HTML5事件源将与服务器建立一个连接,并允许服务器在需要时将更新推送到客户端,而不是发出多个请求。

有关HTML5服务器已发送事件的详细信息,请参阅链接http://www.w3schools.com/html/html5_serversentevents.asp