0

A setInterval runs an anonymous function.

Given the interval id, can I retrieve the function body?

For those curious, I wrote a semi complex function directly in the browser console, and started an interval. I lost the console history because I closed the Firefox inspector, and would like to save myself the effort of rewriting the function. Pressing "Up", no longer brings back my previous entries.

Letharion
  • 4,067
  • 7
  • 31
  • 42
  • Can you show the code of the function and how you are doing it ? and what are you trying to achieve ? It is important to know for StackOverFlowers to answer it in the best way. – Meer Oct 12 '14 at 10:24
  • you can put the function in a .js file and then call it from console. lool at this post --> http://stackoverflow.com/questions/9054870/calling-a-javascript-function-from-console – Meer Oct 12 '14 at 10:27
  • or try this --> function foo(){ return "bar" } console.log( foo() ); – Meer Oct 12 '14 at 10:28
  • 2
    Did you try to use UP button in the console? It will retrieve previous command even if the browser was closed and reopened (at least in Chrome). – Vadim Oct 12 '14 at 10:29
  • about anonymous function see this post --> http://stackoverflow.com/questions/13852457/access-anonymous-function-objects-from-the-console – Meer Oct 12 '14 at 10:32
  • @Vadim yes, I did try that, and as I said, I've lost the history. :) Firebug doesn't keep the history when it's closed. I've added this to the question. – Letharion Oct 12 '14 at 11:20
  • have you tried this extension? http://www.softwareishard.com/blog/consoleexport/ – Meer Oct 12 '14 at 11:28
  • @Letharion The button `History` didn't work as well? – Rahil Wazir Oct 12 '14 at 11:32
  • Is your question how to retrieve the function that is running in an interval, or how to retrieve previous console entries? –  Oct 12 '14 at 12:04
  • @torazaburo I feel that my question is clearly stated above, the history discussion is completely irrelevant (although it could also have helped me) – Letharion Oct 12 '14 at 13:18
  • Given the discussion about history, I checked and realized I actually don't use Firebug, but the built in inspector. – Letharion Oct 12 '14 at 13:20

2 Answers2

1

No, you cannot retrieve the function from the timer ID.

If you wanted to do such a thing regularly, then redefine setInterval to capture the function:

(function() {
    var oldSetInterval = setInterval;        // remember the original setInterval

    setInterval = function(fn, msec) {       // redefine setInterval
        var id = oldSetInterval(fn, msec);   // call the real one
        setInterval.fns[id] = fn;            // remember the fn in an array
        return id;
    };

    setInterval.fns = [];                    // array attached to our setInterval
                                             // remembers functions by timer ID
}());

Use this as in:

var myId = setInterval(function() { alarm(); }, 300000);
alert(setInterval.fns[myId])); // "function() { alarm(); }"
0

SEE THIS all your history saved in firebug

enter image description here

Harutyun Abgaryan
  • 2,013
  • 1
  • 12
  • 15
  • That's cool, but doesn't actually answer the question, which is about javascript intervals, and not firebug and it's history. :) – Letharion Oct 12 '14 at 13:21