【发布时间】:2015-11-30 03:39:14
【问题描述】:
这是代码(使用 Angularjs 和服务器运行 localhost )
index.html
<iframe src="{{Url}}" width="100%" height="500"></iframe>
app.js
$scope.Url = "http://0.0.0.0:8080/index.html";
需要每 30 秒刷新一次 怎么可能?
【问题讨论】:
这是代码(使用 Angularjs 和服务器运行 localhost )
index.html
<iframe src="{{Url}}" width="100%" height="500"></iframe>
app.js
$scope.Url = "http://0.0.0.0:8080/index.html";
需要每 30 秒刷新一次 怎么可能?
【问题讨论】:
我会使用 $timeout 功能。设置一个初始值,然后每 30 秒调用一次 reload 函数并增加一个查询字符串,这样就不会出现缓存问题。如果这不是问题,您可以删除 $scope.seed 内容。
angular
.module('app')
.controller('UrlController', UrlController);
UrlController.$inject = ['$scope', '$timeout'];
function UrlController($scope, $timeout) {
$scope.loadingCounter = 0;
$scope.seed = getUtcDate().getTime();
//adding the counter so the url reload isn't cached.
$scope.baseUrl = 'http://0.0.0.0:8080/index.html';
$scope.Url = 'http://0.0.0.0:8080/index.html';
$scope.getReloadUrl = getReloadUrl;
$scope.onReload = onReload;
function onReload() {
$scope.loadingCounter++;
$scope.Url = $scope.getReloadUrl();
//restart it again.
$timeout($scope.onReload, 30000);
}
function getReloadUrl(){
//create a unique query string
var reloadId = $scope.seed + $scope.loadingCounter;
return $scope.baseUrl + '?reloadId=' + reloadId;
}
//start it up
$scope.onReload();
//helper function
function getUtcDate() {
var now = new Date();
var nowUtc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
return nowUtc;
}
}
【讨论】:
你可以使用 Javascript 和 setInterval 来刷新 Source 的 src...
<script>
window.setInterval("reloadIFrame();", 30000);
function reloadIFrame() {
document.frames["frameNameHere"].location.reload();
}
</script>
【讨论】: