Cómo configurar la variable global en JQuery / javascript

Hi i am new to jquery/javascript. I have created a global variable. I set the value of value of this variable inside function like this:

var arr;
CreatecheckPassword: function() {

                var pas = document.getElementById("pos-password")

                var user = new db.web.Model("res.users").get_func("read")(this.session.uid, ['password']).pipe(function(result) {
                     arr = result.password;
                     alert(arr);//prints the expected value
                alert(arr);//prints nothing

I don't know where i am doing wrong. I tried hard but was unable to find the solution. Can someone help.

preguntado el 10 de marzo de 12 a las 09:03

You have a couple syntax errors there... -

I would guess that your database access function is asynchronous, which means control continues to the alert that prints nothing before the callback is executed and the arr variable is set. -

How can then i set the value of this variable. Can't do without database operation. -

Why can't you move the second alert into a separate function and call it in database callback function? -

Basically you have to restructure your code so that anything that uses the return from an asynchronous function is included within the callback that you pass to the function (or included in a separate function called from that callback). You can probably find some good explanations of Async JavaScript with Google - if you can't find any specifically about database access look at some Ajax tuturials because exactly the same async callback issue occurs with Ajax calls. -

1 Respuestas

jslint or jshint is your friend. Get it and use it on your code to get lint messages.

enter image description here

respondido 10 mar '12, 12:03

But none of the errors/warnings shown have anything to do with the problem, indeed the problem is not caused by a syntax error... - nnnnnn

The problem isn't stated in the question, then. In fact the original question isn't clear at all on the nature of the problem. Also the title of the post is wrong - the question is not at all about how to "Set a global variable". Reading the comments, it's something about asynch invocation of ajax, I think. - queso

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.