我正在尝试创建一个按钮,它为它正在执行的操作提供反馈。从角度上讲,我向服务器发出了一个put请求--此时我更改按钮的状态以指示此状态--然后当我收到响应时,我再次更改按钮的状态,以反映成功或失败1.5秒,然后将其重新设置为按钮单击之前的状态。同样,此时会显示一条消息以反映成功或失败,并且在5秒之后,应该执行额外的操作。显示的消息应该隐藏,如果失败,页面应该被重定向,如果它是成功的。这是我不能工作的最后一部分。
因此,我的问题是,我是否应该能够在另一个setTimeout中放置一个setTimeout?
下面是我的代码(在按钮的函数中):
$scope.resetPassword = function() {
$scope.ShowSuccessMessage = false;
$scope.ShowFailedMessage = false;
var querystring = $location.search();
var dataObject = { email1 : $scope.formEmail1, email2 : $scope.formEmail2, password1: $scope.formPassword1, password2: $scope.formPassword2, token: querystring.token };
var responsePromise = $http.put("/myurl/", dataObject, {});
// change colour and icon of reset button for 1.5 sec
var $el = $("#resetPassword");
var resetButton = 1500;
var resetMessage = 5000;
var originalColor = $el.css("background");
var originalIcon = $el[0].firstChild.className; // get first html element in the button, which is the <i>
$el[0].firstChild.className = "fa fa-circle-o-notch fa-spin";
responsePromise.success(function(dataFromServer, status, headers, config) {
$el.css("background", "#02c66c");
$el[0].firstChild.className = "fa fa-check-circle";
$scope.ShowSuccessMessage = true;
ResetButton($el, $scope, originalColor, originalIcon, resetButton, true, resetMessage);
});
responsePromise.error(function(dataFromServer, status, headers, config) {
$el.css("background", "#d9534f");
$el[0].firstChild.className = "fa fa-times-circle";
$scope.ShowFailedMessage = true;
ResetButton($el, $scope, originalColor, originalIcon, resetButton, false, resetMessage);
});
};
ResetButton = function(el, scope, originalColor, originalIcon, resetButton, success, resetMessage)
{
setTimeout(function() {
el.css("background", originalColor);
el[0].firstChild.className = originalIcon;
ChangeMessageOrChangeLocation(scope, success, resetMessage);
}, resetButton);
}
ChangeMessageOrChangeLocation = function(scope, success, resetMessage)
{
if(success) {
setTimeout(function(){
window.location = '/signin';
}, resetMessage);
}
else {
setTimeout(function() {
scope.ShowFailedMessage = false;
}, resetMessage);
}
}编辑:下面是一个活生生的例子:贪婪喜鹊 (请记住,这是一项正在进行的工作,所以网站上的所有内容都不能正常工作)。只需单击“更改密码”按钮。
发布于 2014-12-16 11:41:07
winfow.setTimeout在角摘要周期之外执行,因此视图没有更新。要么在scope.$apply()之后添加scope.ShowFailedMessage = false;,要么使用$timeout服务而不是setTimeout。第二种方法更好。
在这里查看$timeout服务文档:$timeout
https://stackoverflow.com/questions/27502854
复制相似问题