如何在Jquery中等待函数(没有回调)在执行下一个语句之前运行
How to wait for function (with no callback) to run in Jquery before exceuting next statement?
我使用Parse服务来编写一个用户身份验证系统。Parse提供了注销当前用户的Logout函数,但该函数不提供回调选项。
我的困境是,我需要在异步注销功能完成后运行一个函数。如何在不修改Parse框架的情况下实现这一点呢?
下面是我的代码:function logOut() {
if (getCurrentUsername()) {
Parse.User.logOut();
// Need this to wait till the logOut function completes
loggedIn();
} else {
}
}
function loggedIn() {
if (getCurrentUsername()) {
//Hide the sign in form because it is not needed
$('#sign_in_form').hide();
//Get the username of the current user by calling the function getCurrentUsername()
var currentUsername = getCurrentUsername();
//Write a welcome message to the current user and give a log out option
$('#signed_in_note').html('Welcome <a href="#">'+ currentUsername +'</a> | <a href="#" id="log_out">log out</a>')
//Show the welcome message
$('#signed_in_note').show();
} else {
//If no user is signed in, we show the login form and hide the welcome message
$('#sign_in_form').show();
$('#signed_in_note').hide();
}
}
您可以设置一个简单的计时器来检查用户是否仍在进行身份验证:
var currentUser;
var timerId;
function logOut() {
currentUser = getCurrentUsername();
if (currentUser) {
Parse.User.logOut();
timerId = setInterval(checkAuth, 200);
} else {
//show msg saying already logged out or log an error
}
}
function checkAuth() {
// check if user is logged out
if(!currentUser.authenticated()) {
abortTimer();
loggedIn(); //now this will run only after user is logged out
}
}
function abortTimer() {
clearInterval(timerId);
}
p。S:不要指望这在100%的情况下工作,因为用户可能会在注销调用返回之前关闭浏览器。
相关文章:
- 滚动到容器中的下一个元素-几乎到了
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 与 promise 一起使用时,异步瀑布不执行下一个回调方法
- JavaScript循环等待一些函数返回,然后执行下一个循环
- 插入 html 后执行下一个 js 命令
- 在执行下一个方法之前未完成(javascript jquery google maps v3 api)
- 在执行上一个的处理程序之前检查下一个事件以隐藏自动完成
- 如果请求已被处理,则阻止执行下一个路由器
- 在进入下一个循环迭代之前执行setTImeout操作
- 需要函数等待事件处理程序完成后再执行下一个函数
- Ajax要等到Api完成后才执行下一个函数
- 如何仅在前一个单击完成时执行下一个单击
- 在node.js中,我们能否在执行下一个语句之前强制函数调用完成并返回?
- 等待函数完成后再继续执行$中的下一个循环.每个函数
- 等待Google Earth完成加载,然后在matlab中执行下一个命令
- 使用jQuery执行发布请求并显示下一个页面
- 在执行下一个 AJAX 之前执行 Ajax 内部条件
- JS按顺序执行函数,同时将下一个函数作为参数传递
- 如何在Jquery中等待函数(没有回调)在执行下一个语句之前运行
- 链式jQuery.then() -每个下一个只在前一个解析完成后执行