Ulrik M
Ulrik M

Reputation: 163

Javascript, function inside function problem

Hey, I was working with a javascript project and reached a problem that I just don't understand. Here is the code, it's not the one that I use in my project, but it's a simplified version.

var x;

function FetchBox() {alert("Worked");}

function A(m,n) { 
    x = new XMLHttpRequest(); 
    x.open("GET", m, true);
    x.onreadystatechange=function(){
        n(); 
        x.send();
    };
}

A("http://jsfiddle/echo/xml/", FetchBox);

I can easily change the function to make it work:

function A(m,n) { 
    x = new XMLHttpRequest(); 
    x.open("GET", m, true);
    x.onreadystatechange=n();x.send();
}

But in my more complex version I want to add a readyState function and a few other things.

function A(m,n) { 
    x = new XMLHttpRequest(); 
    x.open("GET", m, true);
    x.onreadystatechange=
        if(x.readyState===4){
            n(); 
            x.send();
        };
}

Why can't I include a function inside this function? JsFiddle link: http://jsfiddle.net/M6Upv/17/

Have a good weekend, Ulrik

Upvotes: 1

Views: 3687

Answers (5)

Radek
Radek

Reputation: 8376

Try that way.

function A(m,n) { 
    x = new XMLHttpRequest(); 
    x.open("GET", m, true);
    x.onreadystatechange = function() {
        if(x.readyState===4) {
            n(); 
            //x.send(); //look below
        };
    }
    x.send() //I think, it should be here
}

Upvotes: 5

user69820
user69820

Reputation:

You need to move the send() to outside of the onreadystatechange event handler:

function A(m,n) { 
    x = new XMLHttpRequest(); 
    x.open("GET", m, true);
    x.onreadystatechange = function() {
        if(x.readyState === 4) {
            n(); 
        };
    }
    x.send();
}

Upvotes: 1

Nalum
Nalum

Reputation: 4213

You need to run x.send() before the x.onreadystatechange() can be called.

function A(m, n) {
    x = new XMLHttpRequest();
    x.open("GET", m, true);
    x.onreadystatechange = function() {
        if (x.readyState == 4) {
            n();
        }
    };
    x.send();
}

Upvotes: 1

Quentin
Quentin

Reputation: 943214

It really isn't clear what you are trying to do, but here are some things you are doing wrong:

onreadystate should be a function that gets called whenever the readystate changes.

In your last example, you are trying to assign an if statement to it, which makes no sense at all.

In previous versions you are calling send() inside it — which makes no sense, as it isn't going to start firing until after send() has been called.

In all versions, you are defining x as a global, which is a good way to trigger race conditions.

Upvotes: 1

yan
yan

Reputation: 20982

What you're doing when you change it is simply calling n(), assigning its return value to x.onreadystatechange and then calling x.send(). Wrapping your code in function() { .. } delays the computation to when the callback is actually triggered. You'd still need to wrap your code in function() { .. } to make this work.

Upvotes: 1

Related Questions