0

I'm trying to debug a scope issue in Javascript, and there is definitely something I'm not getting here. I would be happy if someone could help.

Among other variables that I declare in the global scope:

var mesh1_min, mesh1_max;

Then I use them inside init().

function init() {
    fetchStl('Octocat-v1.stl', function (stlBinary) {
      geometry1 = parseStlBinary(stlBinary);
      center(geometry1);
      mesh1BB = geometry1.computeBoundingBox();
      mesh1_min = geometry1.boundingBox.min;
      mesh1_max = geometry1.boundingBox.max;
    });
    alert(mesh1_min)
}

And for some reason I get undefined when mesh1_min gets out of scope. It must have something to do with it getting out of scope, but why is that, because I defined it as a global variable on the top.

Thanks.

jhagege
  • 1,486
  • 3
  • 22
  • 36
  • It's not a scope issue, it's a time issue. You're calling `alert()` before the callback function has run. – Barmar Jul 02 '14 at 19:55

1 Answers1

0

If fetchStl is asynchronous, and nothing else changed the value of mesh1_min before that code is run, it should still hold undefined. Do you know if that's the case?

Felipe Ruiz
  • 181
  • 1
  • 6
  • It is definitely asynchronous. Thank you very much for pointing this out, I didn't think about it.. I'm in for some callback hell session :-) – jhagege Jul 02 '14 at 20:14