Javascript SetTimeout内的SetTimeout未触发
我正在尝试创建一个按钮,该按钮为它正在执行的操作提供反馈。在Angular中,我向服务器发出一个put请求——此时我更改按钮的状态以表明这一点——然后当我收到响应时,我再次更改按钮的状态以反映成功或失败1.5秒,然后将其重置为单击按钮之前的状态。同时,此时将显示一条消息,以反映成功或失败,并在5秒钟后执行其他操作。如果失败,则显示的消息应隐藏,如果成功,则页面应重定向。这是最后一部分,我不能去工作 因此,我的问题是,我是否应该能够将一个setTimeout放入另一个setTimeout中 下面是我在按钮函数中的代码:Javascript SetTimeout内的SetTimeout未触发,javascript,angularjs,Javascript,Angularjs,我正在尝试创建一个按钮,该按钮为它正在执行的操作提供反馈。在Angular中,我向服务器发出一个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);
}
}
编辑:这里有一个活生生的例子:请记住这是一个正在进行的工作,所以不是网站上的所有东西都能工作。只需单击更改密码按钮…winfow.setTimeout在角度摘要周期之外执行,因此视图未更新。在scope.ShowFailedMessage=false之后添加scope.$apply;或者使用$timeout服务代替setTimeout。第二种方法更好
检查此处的$timeout服务文档:您应该能够级联它们。在超时内调用超时不会有问题。这只是功能。这调用了另一个函数。@peernohell我就是这么想的,知道我的第二个setTimeout没有触发的原因吗?可能是重复的,谢谢你的解释,非常感谢!