2

I have code that looks like this:

function myEventHandler() {
    inMyEventHandler = true;
    longRunningStuff();
    inMyEventHandler = false;
}

This works great, except on the iPad where Safari Mobile occasionally times out my Javascript with an error. So longRunningStuff() dies and inMyEventHandler never gets cleared. This is very bad, because inMyEventHander absolutely cannot be set if we're outside this function, or Bad Things(tm) happen.

Ideally, I could just check from deep within longRunningStuff whether myEventHandler is above it in the call stack, and this would take care of itself. I can't find a way to do that... Hints?

Ben Dilts
  • 10,535
  • 16
  • 54
  • 85

3 Answers3

2
  1. (Simplest) You can check arguments.callee.caller when in longRunningStuff:

    function longRunningStuff() {
      if (arguments.callee.caller === myEventHandler) {
        // take care
      }
    }
    
  2. Another variant:

    function myEventHandler() {
      // code
    }
    myEventHandler.disable_in_longRunningStuff = true;
    function longRunningStuff() {
      if (arguments.callee.caller.disable_in_longRunningStuff) {
        // take care
      }
    }
    
  3. The longest way - retrieve call stack from https://github.com/eriwen/javascript-stacktrace

Victor
  • 3,669
  • 3
  • 37
  • 42
1

I have used the following method for years to manually inspect the callstack. I haven't updated it in a while and never tried it on ipad or mobile safari, so whether or not it will even work for you i can't tell.

Maybe you can use it for inspiration:

function logStackTrace(levels) {
    var c = console;
    var callstack = [];
    var isCallstackPopulated = false;
    try {
        throw new Error();
    } catch (e) {
        if (e.stack) { //Firefox
            var lines = e.stack.split('\n');
            for (var i = 0, len = lines.length; i < len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    callstack.push(lines[i]);
                }
            }
            //Remove call to logStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        }
        else if (window.opera && e.message) { //Opera
            var lines = e.message.split('\n');
            for (var i = 0, len = lines.length; i < len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    var entry = lines[i];
                    //Append next line also since it has the file info
                    if (lines[i + 1]) {
                        entry += " at " + lines[i + 1];
                        i++;
                    }
                    callstack.push(entry);
                }
            }
            //Remove call to logStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        }
    }
    if (!isCallstackPopulated) { //IE and Safari
        var currentFunction = arguments.callee.caller;
        while (currentFunction) {
            var fn = currentFunction.toString();
            var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf("(")) || "anonymous";
            callstack.push(fname);
            currentFunction = currentFunction.caller;
        }
    }
    if (levels) {
        c.log(callstack.slice(0, levels).join('\n'));
    }
    else {
        c.log(callstack.join('\n'));
    }
};
Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68
0

As of iOS 6 a real web inspector is available. See below, or this link. https://stackoverflow.com/a/12762449/72428

Update!!! On OS X you can use the Safari web inspector on the iOS Simulator AND iOS 6 devices.

  1. First enable the Developer menu in Safari.
  2. Next, enable remote debugging on your iOS device (or simulator).

    Settings > Safari > Advanced > Web Inspector (ON)
    
  3. Go back to Safari on your device.
  4. Go back to your computer, click the Developer menu, and select your device (e.g. iPhone Simulator, iPhone)

Note: You'll see your device in the Developer menu ONLY when Safari is active and running.

Enjoy!

Community
  • 1
  • 1
Blaine
  • 1,107
  • 4
  • 12
  • 23