Javascript函数是真还是假
你好,我有这个功能:Javascript函数是真还是假,javascript,jquery,Javascript,Jquery,你好,我有这个功能: function makeTweet(params) { var tweetID = params.tweetID; var tweetDate = params.tweetUrl; var tweetText = params.tweetText; var tweetUrl = params.tweetUrl; var authorName = params.tweetDate; var authorNickname = p
function makeTweet(params) {
var tweetID = params.tweetID;
var tweetDate = params.tweetUrl;
var tweetText = params.tweetText;
var tweetUrl = params.tweetUrl;
var authorName = params.tweetDate;
var authorNickname = params.authorNickname;
var authorAvatar = params.authorAvatar;
if ( haveOauth() == true ) {
alert("We have the account, make the reply");
}else{
alert("We do not have it");
}
}
它应该得到下面函数的结果,并了解它是否返回true或false,并在此基础上执行特定操作
我该怎么做?无论我现在做什么,都会返回相同的结果。
这里有点不对劲。
谢谢
不建议使用
async:false
,但您可以尝试替换
if(data) {
return false;
}
与
但是为什么不让haveSweard进行自己的回调呢
function haveOauth(callback) {
var user_id = "1";
var data = $.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
success : function(data) {
callback(data);
}
});
}
然后
function makeTweet(params) {
var tweetID = params.tweetID;
var tweetDate = params.tweetUrl;
var tweetText = params.tweetText;
var tweetUrl = params.tweetUrl;
var authorName = params.tweetDate;
var authorNickname = params.authorNickname;
var authorAvatar = params.authorAvatar;
haveOauth(function(data) {
if (data) {
alert("We have the account, make the reply");
}else{
alert("We do not have it");
}
});
}
试试这个:
function haveOauth() {
var result = true;
var user_id = "1";
var data = $.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
async: false,
success : function(data) {
if(data) {
result = false;
}
}
});
return result;
}
与迈克尔·斯旺的回答类似——我认为你永远也无法回到现实
function haveOauth() {
var user_id = "1";
var data = $.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
async: false,
success : function(data) {
if(data) {
return false;
}
}
});
return true;
}
正如@adamrack所指出的,haveOauth函数包含一个不建议同步的AJAX调用。(除此之外,在传递给
$的回调函数中,您的返回
。ajax
没有实现您想要的功能,并且您在某个地方缺少一个返回true
——请参阅@MichaelSwan提供的解决方案。)
您可以使用通过签入haveOauth的结果的回调来使代码异步:
function haveOauth(callback) {
var user_id = "1";
$.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
success : function(data) {
if (data) { // or whatever check needs to be made
callback(true); // signal that authenticated
} else
callback(false); // signal that not authenticated
}
});
}
然后在makeTweet
中使用该函数:
haveOauth(function(isAuthenticated) {
if (isAuthenticated)
alert("We have the account, make the reply");
else
alert("We do not have it");
});
不,下面的函数应该返回false或true。不是第一个,而是第二个函数。请参见第二个函数中的内容:if(data){return false;}@DiegoP.:返回将立即发生,您无法返回异步请求的结果值(这可能会导致错误)。但是,您可以返回一个对象。
function haveOauth(callback) {
var user_id = "1";
$.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
success : function(data) {
if (data) { // or whatever check needs to be made
callback(true); // signal that authenticated
} else
callback(false); // signal that not authenticated
}
});
}
haveOauth(function(isAuthenticated) {
if (isAuthenticated)
alert("We have the account, make the reply");
else
alert("We do not have it");
});