gpt4 book ai didi

javascript - SetTimeout 内的 SetTimeout 未触发

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:31:33 28 4
gpt4 key购买 nike

我正在尝试创建一个按钮,以反馈其正在执行的操作。在 Angular 中,我向服务器发出一个放置请求——此时我更改按钮的状态以指示这一点——然后当我收到响应时,我再次更改按钮的状态以反射(reflect)成功或失败 1.5 秒,然后重置它回到点击按钮之前的状态。同样在此时显示一条消息以反射(reflect)成功或失败,再过 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);
}
}

编辑:这是一个活生生的例子:Greedy Magpie (请记住,这是一项正在进行的工作,因此并非网站上的所有内容都有效。)只需单击更改密码按钮...

最佳答案

winfow.setTimeout 在 Angular 摘要周期之外执行,因此 View 未更新。在 scope.ShowFailedMessage = false; 之后添加 scope.$apply() 或使用 $timeout 服务而不是 setTimeout。第二种方法更好。

在此处查看 $timeout 服务文档:$timeout

关于javascript - SetTimeout 内的 SetTimeout 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27502854/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com