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 plex 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: /
Have a good weekend, Ulrik
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 plex 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/M6Upv/17/
Have a good weekend, Ulrik
Share Improve this question asked Feb 18, 2011 at 15:36 Ulrik MUlrik M 1632 silver badges10 bronze badges5 Answers
Reset to default 5Try 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
}
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 putation to when the callback is actually triggered. You'd still need to wrap your code in function() { .. }
to make this work.
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.
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();
}
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();
}