简体   繁体   中英

Ajax request / unable to empty the global variable

I am very new to Javascript/jquery and async. I read a lot in this forum. But i did not find a solution for my problem.

i get a JS File (logger.js) from a data logger like:

ab[cd++]="data1"
ab[cd++]="data2"
ab[cd++]="data3"
...

i try to load this file in a mobile App.

var ab = [];
var cd = 0;

with an ajax call:
    $.get("logger.js", function() {
        alert(ab.length); //3  works fine for the first request 
    });
....
ab.length = 0; //doesnt work.

on the second request ab.length is 6, on the third request ab.length is 9 and so on.

Where is the place to empty ab?

You should redefine ab as an empty array:

ab = [];

Be aware that you are performing an ASYNC call, so make sure that you redefine your ab variable inside the callback function (my guess, i dont know where you are using it):

$.get("logger.js", function() {
    //do something with ab here and then redefine it.
    ab = [];
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM