简体   繁体   中英

numeric comparison giving wrong result (javascript)

The following code is giving false result all the time. I think i am comparing two numeric variables. is that not so?

var request; var getfilesize; var maxdisplaysize=2097152;
    request = $.ajax({
    type: "HEAD",
    url: getfile,
    success: function () {
      getfilesize = request.getResponseHeader("Content-Length");
      console.log("Size is" + getfilesize+'xxx');
    }
    });
    parseInt( getfilesize, 10 );
if ( getfilesize < maxdisplaysize ) {
    $("#displayfile").load(getfile);
    console.log('file size less then 2mb 2097152bytes');
} else {
    $("#displayfile").append('<p>sorry large file<p>');
}

Pl advice why the if condition is always evaluating to be false?

Your call to parseInt() has no effect, as you don't do anything with the result.

getfilesize = parseInt(getfilesize, 10);

is what you want. The parseInt() function does not (and cannot ) modify the first parameter.

Now, that said, the next problem you're going to want to deal with is the fact that your ajax call is asynchronous . You need to put that code that uses the file size inside the "success" callback.

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