Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 定期使用相同的URL更新背景图像_Javascript_Html_Angularjs_Background Image_Ng Style - Fatal编程技术网

Javascript 定期使用相同的URL更新背景图像

Javascript 定期使用相同的URL更新背景图像,javascript,html,angularjs,background-image,ng-style,Javascript,Html,Angularjs,Background Image,Ng Style,我正在开发一个angular JS应用程序。我的身体背景必须根据服务器托管的图像每分钟改变一次 在我的HTML中,我使用ng样式设置背景图像: <body ng-controller="mainCTRL" ng-style="{backgroundImage: 'url(' + imageURL + ')'}"> 它工作得很好,但现在我需要刷新图像。托管图像每分钟都会更改,但它始终托管在同一URL上。为此,我在JS代码中添加了以下内容: startRefresh(); funct

我正在开发一个angular JS应用程序。我的身体背景必须根据服务器托管的图像每分钟改变一次

在我的HTML中,我使用ng样式设置背景图像:

<body ng-controller="mainCTRL" ng-style="{backgroundImage: 'url(' + imageURL + ')'}">
它工作得很好,但现在我需要刷新图像。托管图像每分钟都会更改,但它始终托管在同一URL上。为此,我在JS代码中添加了以下内容:

startRefresh();

function startRefresh() {
    $interval(test, );
}

test = function () {
    $scope.urlImage = '';
    $timeout(function () { 
        $scope.urlImage = serverURL + '/Images/background.png'; 
    }, 1000);
};
计时器工作,每分钟调用一次测试函数,但图像始终不变

我想有一个解决方案来改变图像,即使URL是相同的

--


我是一个javascript初学者,很抱歉我的英语。谢谢您

在测试功能中尝试以下更改:

test = function () {
    $scope.urlImage = '';
    $timeout(function () { 
        $scope.urlImage = ''; // add this line
        $scope.urlImage = serverURL + '/Images/background.png'; 
    }, 1000);
};

希望这对你有用……)

在测试功能中尝试以下更改:

test = function () {
    $scope.urlImage = '';
    $timeout(function () { 
        $scope.urlImage = ''; // add this line
        $scope.urlImage = serverURL + '/Images/background.png'; 
    }, 1000);
};

希望这对你有用……)

尝试将url更改为以下内容:

var currentTime = new Date().getTime();
$scope.urlImage = serverURL + '/Images/background.png?' + currentTime;

尝试将url更改为以下内容:

var currentTime = new Date().getTime();
$scope.urlImage = serverURL + '/Images/background.png?' + currentTime;

两个建议:确保服务器发送的头不缓存图像(至少不超过一分钟),并在图像URL后添加一个随机数作为请求参数,以破坏浏览器的缓存,例如
$scope.urlmimage=serverURL+'/Images/background.png?r='+Math.random()两个建议:确保服务器发送头不缓存图像(至少不超过一分钟),并在图像URL后添加一个随机数作为请求参数,以破坏浏览器的缓存,例如
$scope.urlmimage=serverURL+'/Images/background.png?r='+Math.random()谢谢!我在3分钟内就把你的答案有效了非常感谢。我在3分钟内就把你的答案有效了我尝试了这个,它不起作用,因为第二次更改后urlImage是相同的。我尝试了这个,它不起作用,因为第二次更改后urlImage是相同的。