Javascript global variable undeclared problem
up vote
0
down vote
favorite
In the mentioned sample code I'm trying to get a value for val variable. I declared it with global scope. And add some console log to verify values. But inside of the function it assign value without problem. But out of the function val is undeclared. why is that?
$.validator.addMethod("serialverify", function(){
var val;
$("#serialno").keyup(function(){
serial().done(function(data){
console.log('final = ' + data);
val = data;
console.log(val);
});
}); console.log(val);
return val;
}, "Please enter valid serial code");
javascript
add a comment |
up vote
0
down vote
favorite
In the mentioned sample code I'm trying to get a value for val variable. I declared it with global scope. And add some console log to verify values. But inside of the function it assign value without problem. But out of the function val is undeclared. why is that?
$.validator.addMethod("serialverify", function(){
var val;
$("#serialno").keyup(function(){
serial().done(function(data){
console.log('final = ' + data);
val = data;
console.log(val);
});
}); console.log(val);
return val;
}, "Please enter valid serial code");
javascript
2
"I declared it with global scope." No you didn't.
– Robby Cornelissen
Nov 19 at 7:10
1
doesn't seem like yourvalvariable is global
– Alon Yampolski
Nov 19 at 7:12
return valwill always produce null as your assignment is taking place in a presumably asynchronous callbackserial().done()
– Abana Clara
Nov 19 at 7:17
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In the mentioned sample code I'm trying to get a value for val variable. I declared it with global scope. And add some console log to verify values. But inside of the function it assign value without problem. But out of the function val is undeclared. why is that?
$.validator.addMethod("serialverify", function(){
var val;
$("#serialno").keyup(function(){
serial().done(function(data){
console.log('final = ' + data);
val = data;
console.log(val);
});
}); console.log(val);
return val;
}, "Please enter valid serial code");
javascript
In the mentioned sample code I'm trying to get a value for val variable. I declared it with global scope. And add some console log to verify values. But inside of the function it assign value without problem. But out of the function val is undeclared. why is that?
$.validator.addMethod("serialverify", function(){
var val;
$("#serialno").keyup(function(){
serial().done(function(data){
console.log('final = ' + data);
val = data;
console.log(val);
});
}); console.log(val);
return val;
}, "Please enter valid serial code");
javascript
javascript
asked Nov 19 at 7:09
Adam
165
165
2
"I declared it with global scope." No you didn't.
– Robby Cornelissen
Nov 19 at 7:10
1
doesn't seem like yourvalvariable is global
– Alon Yampolski
Nov 19 at 7:12
return valwill always produce null as your assignment is taking place in a presumably asynchronous callbackserial().done()
– Abana Clara
Nov 19 at 7:17
add a comment |
2
"I declared it with global scope." No you didn't.
– Robby Cornelissen
Nov 19 at 7:10
1
doesn't seem like yourvalvariable is global
– Alon Yampolski
Nov 19 at 7:12
return valwill always produce null as your assignment is taking place in a presumably asynchronous callbackserial().done()
– Abana Clara
Nov 19 at 7:17
2
2
"I declared it with global scope." No you didn't.
– Robby Cornelissen
Nov 19 at 7:10
"I declared it with global scope." No you didn't.
– Robby Cornelissen
Nov 19 at 7:10
1
1
doesn't seem like your
val variable is global– Alon Yampolski
Nov 19 at 7:12
doesn't seem like your
val variable is global– Alon Yampolski
Nov 19 at 7:12
return val will always produce null as your assignment is taking place in a presumably asynchronous callback serial().done()– Abana Clara
Nov 19 at 7:17
return val will always produce null as your assignment is taking place in a presumably asynchronous callback serial().done()– Abana Clara
Nov 19 at 7:17
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.
However, the statement return val is happening synchronously.
Suggestion -
You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -
......done( function(data) { ..... resolve(val); })
or declare var val; as global that is outside, the addMethod function
oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.
However, the statement return val is happening synchronously.
Suggestion -
You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -
......done( function(data) { ..... resolve(val); })
or declare var val; as global that is outside, the addMethod function
oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24
add a comment |
up vote
3
down vote
accepted
There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.
However, the statement return val is happening synchronously.
Suggestion -
You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -
......done( function(data) { ..... resolve(val); })
or declare var val; as global that is outside, the addMethod function
oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.
However, the statement return val is happening synchronously.
Suggestion -
You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -
......done( function(data) { ..... resolve(val); })
or declare var val; as global that is outside, the addMethod function
There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.
However, the statement return val is happening synchronously.
Suggestion -
You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -
......done( function(data) { ..... resolve(val); })
or declare var val; as global that is outside, the addMethod function
answered Nov 19 at 7:14
Aseem Upadhyay
1,056621
1,056621
oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24
add a comment |
oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24
oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24
oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369835%2fjavascript-global-variable-undeclared-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
"I declared it with global scope." No you didn't.
– Robby Cornelissen
Nov 19 at 7:10
1
doesn't seem like your
valvariable is global– Alon Yampolski
Nov 19 at 7:12
return valwill always produce null as your assignment is taking place in a presumably asynchronous callbackserial().done()– Abana Clara
Nov 19 at 7:17