var userIdD = $.get('http://someurl.com/options.php', function(abc) {
var abc = $(abc);
var link = $(abc).find('a:contains(View My Profile)');
//console.log (link);
var userID = $(link).attr('href');
var userId = $(userID);
console.log(userId);
console.log(userID);
});
console.log(userIdD);
console.log(userId);
I can`t start using variables "userIdD" & "userId" outside function "abc". Inside it, it works greatly. Can someone help me to use them outside it? where I'm wrong?
我不能在函数“abc”之外开始使用变量“userIdD”和“userId”。在它内部,它非常有效。有人可以帮我在外面使用它们吗?哪里我错了?
2 个解决方案
#1
0
Declare your variables outside the function:
在函数外声明你的变量:
var userID, userId;
var userIdD = $.get('http://someurl.com/options.php', function(abc) {
var abc = $(abc)
var link = $(abc).find('a:contains(View My Profile)')
//console.log (link)
userID = $(link).attr('href');
userId = $(userID)
console.log(userId)
console.log(userID)
})
console.log(userIdD)
console.log(userId)
#2
0
The problem is that userIdD
is set asynchronously. The stuff that happens inside the function call happens after the stuff outside the function call.
问题是userIdD是异步设置的。函数调用内发生的事情发生在函数调用之外的东西之后。
Here's a simplified example which you can run:
这是一个可以运行的简化示例:
$.get('http://jsonplaceholder.typicode.com', function(){
alert('Stuff inside function happening');
});
alert('Stuff outside function happening');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Now, if we look at your code, we'll see that you're trying to console.log variables that are set inside the function call.
现在,如果我们查看您的代码,我们将看到您正在尝试在函数调用中设置的console.log变量。
To go around the asynchronicity, you can make use of promises. Let's have a look at how that works...
要绕过异步,你可以利用承诺。我们来看看它是如何工作的......
var valuesPromise = $.get('http://jsonplaceholder.typicode.com').then(function(serverResponse){
return { objectId: 123 }; // return stuff you want to use later
});
// later in your code, you can make use of the returned value...
valuesPromise.then(function(returnedValue){
alert(returnedValue.objectId); // alerts '123'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#1
0
Declare your variables outside the function:
在函数外声明你的变量:
var userID, userId;
var userIdD = $.get('http://someurl.com/options.php', function(abc) {
var abc = $(abc)
var link = $(abc).find('a:contains(View My Profile)')
//console.log (link)
userID = $(link).attr('href');
userId = $(userID)
console.log(userId)
console.log(userID)
})
console.log(userIdD)
console.log(userId)
#2
0
The problem is that userIdD
is set asynchronously. The stuff that happens inside the function call happens after the stuff outside the function call.
问题是userIdD是异步设置的。函数调用内发生的事情发生在函数调用之外的东西之后。
Here's a simplified example which you can run:
这是一个可以运行的简化示例:
$.get('http://jsonplaceholder.typicode.com', function(){
alert('Stuff inside function happening');
});
alert('Stuff outside function happening');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Now, if we look at your code, we'll see that you're trying to console.log variables that are set inside the function call.
现在,如果我们查看您的代码,我们将看到您正在尝试在函数调用中设置的console.log变量。
To go around the asynchronicity, you can make use of promises. Let's have a look at how that works...
要绕过异步,你可以利用承诺。我们来看看它是如何工作的......
var valuesPromise = $.get('http://jsonplaceholder.typicode.com').then(function(serverResponse){
return { objectId: 123 }; // return stuff you want to use later
});
// later in your code, you can make use of the returned value...
valuesPromise.then(function(returnedValue){
alert(returnedValue.objectId); // alerts '123'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>