These are my three functions that I am using in javascript :
这些是我在javascript中使用的三个函数:
function postRequest()
{
var xmlHttp;
if(window.XMLHttpRequest)
{ // For Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{ // For Internet Explorer
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('GET', 'effort.php', true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
get_string(xmlHttp.responseText);
dij();
}
}
xmlHttp.send(null);
}
function get_string(str)
{
get_integer = str.split(" ");
for(var i=0;i<214;i++)
{
vertex_i[j] = get_integer[i]*1;
j++;
}
j=0;
for(var i=214;i<427;i++)
{
vertex_f[j] = get_integer[i]*1;
j++;
}
j=0;;
for(var i=427;i<517;i++)
{
x[j] = get_integer[i]*1;
j++;
}
j=0;
for(var i=517;i<607;i++)
{
y[j] = get_integer[i]*1;
j++;
}
for(var m=0;m<90;m++)
{
for(var n=0;n<90;n++)
{
L[m][n] = -1;
}
}
for(var m=0;m<212;m++)
{
x1 = x[vertex_i[m]];
x2 = x[vertex_f[m]];
y1 = y[vertex_i[m]];
y2 = y[vertex_f[m]];
L[vertex_i[m]][vertex_f[m]] = parseInt(find_dist(x1,x2,y1,y2));
}
}
function point_it(event)
{
postRequest();
}
namely : point_it(event)
,then postRequest();
and finally dij();
即:point_it(事件),然后是postRequest();最后是dij();
In these functions I use the data in three globally defined arrays,the elements of whose are derived from the data sent by the server(get_string function).
在这些函数中,我使用三个全局定义的数组中的数据,其元素派生自服务器发送的数据(get_string函数)。
if I call dij()
function from within the postRequest()
function(after the get_string function I am able to access the correct data within the arrays.
如果我从postRequest()函数中调用dij()函数(在get_string函数之后我能够访问数组中的正确数据)。
However if I call it immediately after the postRequest()
function the value of elements in the array become equal to null. I am unable to understand the proper reason for this and have tried several ways to get through but with no progress. CAn sm1 please help me out !
但是,如果我在postRequest()函数之后立即调用它,则数组中元素的值将变为null。我无法理解这个的正当理由,并尝试了几种方法来通过但没有进展。 CAn sm1请帮帮我!
3 个解决方案
#1
0
postRequest
fires an asynchronous request to the server. Calling a function directly after it doesnt mean that the request has finished and youve doen anything with the response data. It works inside postRequest
because that where you actually handle processing the request and response.
postRequest向服务器发出异步请求。在它之后直接调用函数并不意味着请求已经完成,并且您已经对响应数据做了任何事情。它在postRequest中工作,因为您实际处理请求和响应的位置。
if you want to do this all from within point it i would recommend doing the following:
如果你想从内到外完成这一点,我建议你做以下事情:
function postRequest(callback)
{
var callbackFunc = callback||null;
var xmlHttp;
if(window.XMLHttpRequest)
{ // For Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{ // For Internet Explorer
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('GET', 'effort.php', true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
get_string(xmlHttp.responseText);
if(callbackFunc){
callbackFunc();
}
}
}
xmlHttp.send(null);
}
function point_it(event)
{
postRequest(dij);
}
this allows you to vary the callback that uses the array thats been populated by the post request and in a away that it always fires after that request cycle is complete.
这允许您改变使用由post请求填充的数组的回调,以及在请求周期完成后始终触发的远程回调。
#2
0
XMLHttpRequest
is asynchronous.
XMLHttpRequest是异步的。
That means, it will return as soon as the request was send. If you now call dij()
the request will still be pending and get_string
wasn't called yet.
这意味着,它将在请求发送后立即返回。如果现在调用dij(),请求仍将处于挂起状态,并且尚未调用get_string。
As soon as the requests completes, the callback will be called, and then execute get_string
.
请求完成后,将立即调用回调,然后执行get_string。
You need to leave dij()
inside the callback too.
你需要在回调中留下dij()。
Visually:
视觉:
-
postRequest
is made, sets the callback, but does not execute it,postRequest
then returns - 发出postRequest,设置回调,但不执行它,postRequest然后返回
- the code after the call to
postRequest
executes - 执行postRequest调用后的代码
- some time passes...
- 一段时间过去了......
- the
XMLHttpRequest
finally completes and the callback executes, which now callsget_string
- XMLHttpRequest最终完成并执行回调,现在调用get_string
#3
0
Global variables are not a good practice. It is better to combine your get_string and dij functions into one with your arrays as local variables inside the single function.
全局变量不是一个好习惯。最好将get_string和dij函数组合成一个数组作为单个函数内的局部变量。
#1
0
postRequest
fires an asynchronous request to the server. Calling a function directly after it doesnt mean that the request has finished and youve doen anything with the response data. It works inside postRequest
because that where you actually handle processing the request and response.
postRequest向服务器发出异步请求。在它之后直接调用函数并不意味着请求已经完成,并且您已经对响应数据做了任何事情。它在postRequest中工作,因为您实际处理请求和响应的位置。
if you want to do this all from within point it i would recommend doing the following:
如果你想从内到外完成这一点,我建议你做以下事情:
function postRequest(callback)
{
var callbackFunc = callback||null;
var xmlHttp;
if(window.XMLHttpRequest)
{ // For Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{ // For Internet Explorer
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('GET', 'effort.php', true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
get_string(xmlHttp.responseText);
if(callbackFunc){
callbackFunc();
}
}
}
xmlHttp.send(null);
}
function point_it(event)
{
postRequest(dij);
}
this allows you to vary the callback that uses the array thats been populated by the post request and in a away that it always fires after that request cycle is complete.
这允许您改变使用由post请求填充的数组的回调,以及在请求周期完成后始终触发的远程回调。
#2
0
XMLHttpRequest
is asynchronous.
XMLHttpRequest是异步的。
That means, it will return as soon as the request was send. If you now call dij()
the request will still be pending and get_string
wasn't called yet.
这意味着,它将在请求发送后立即返回。如果现在调用dij(),请求仍将处于挂起状态,并且尚未调用get_string。
As soon as the requests completes, the callback will be called, and then execute get_string
.
请求完成后,将立即调用回调,然后执行get_string。
You need to leave dij()
inside the callback too.
你需要在回调中留下dij()。
Visually:
视觉:
-
postRequest
is made, sets the callback, but does not execute it,postRequest
then returns - 发出postRequest,设置回调,但不执行它,postRequest然后返回
- the code after the call to
postRequest
executes - 执行postRequest调用后的代码
- some time passes...
- 一段时间过去了......
- the
XMLHttpRequest
finally completes and the callback executes, which now callsget_string
- XMLHttpRequest最终完成并执行回调,现在调用get_string
#3
0
Global variables are not a good practice. It is better to combine your get_string and dij functions into one with your arrays as local variables inside the single function.
全局变量不是一个好习惯。最好将get_string和dij函数组合成一个数组作为单个函数内的局部变量。