Skip to content

Stop infinity with the power of $.maxStop()

Tired of infinite loops? Infinite recursion got you stuck? Tired of introducing meaningless count variables just beat the scourge of an unresponsive script?

Then I’ve got the tool for you. Hot off the assembly lines, tested thoroughly in rugged environments across the landscape, $.maxStop() will stop the horizontal eight in its tracks!

To use:

if ($.maxStop() ) {return;}  //in a loop replace return with break

Kills recursion and infinite loops.

Here is the code for the plugin:


$.maxStop = $.maxStop || (function () {
    var count, max;
    var defaultMax = 1000;
    ret = function (numMsg) {
        if (typeof numMsg === "number") {
            if (numMsg >0) {
                if (numMsg !== max) { //
                    count = -1;
                    max = numMsg;
                }
            } else if (numMsg <0 ){
                count += numMsg;
            } else {
                if (max !== defaultMax) {
                    count = -1;
                    max = defaultMax;  //0 resets to default max. 1 is minimal count
                }
            }
        } else if (typeof numMsg === "string") {
            $.log(numMsg, ":", count, "/", max);
        }  else if (numMsg) {
            $.log(count, "/", max, numMsg);
        }
        count += 1;
        if (count <= max) {
            return false;
        } else {
            return true;
        }
    };
    return ret;
})();

So the function takes in a number, a string, else, or ignores. A number will set the maximum and if it actually changes, reset the count. What’s great is that you can use this one statement as demonstrated below in a repeating situation and it won’t reset. So in a single statement, you can set how much iteration you can tolerate and check whether it has been done.

The string will put it in debug mode. It will print current count and a message. You can output any object state by stringifying; see example.

Anything else will also put in debug and prints to console the object for clicking through and examining.

Each call will add 1 to the counter–hence resetting counter to -1. And then return true if stoppage is needed, false otherwise.

Here are some examples:

$.stringify = $.stringify || function (obj) {  //encapsulate for older browsers not to choke without JSON.stringify method
    if (JSON && JSON.stringify) {
        return JSON.stringify (obj);
    } else {
        return "no JSON; upgrade broswer!";
    }
}

$.tests.load("$.maxStop", [
    function () {
        $.maxStop(10);
        var localCount = 0;
        while (1) {
            if ($.maxStop($.stringify({localCount:localCount})+"loop going")) {$.maxStop("loop will terminate"); break;}
            localCount += 1;
        }
        return ["while", localCount, 10];
    },
    function () {
        var count = 0;
        var f = function me () {
            if ($.maxStop(0)) {$.maxStop("recursion stopping"); return;}
            count+= 1;
            me();
        }
        f();
        return ["recursion", count, 1001];
    },
    function () {
        $.maxStop(30);
        var count = 0;
        var f = function me () {
            if ($.maxStop()) {$.maxStop("recursion"); return;}
            count+= 1;
            me();
        }
        while (1) {
            if ($.maxStop()) {$.maxStop("loop"); break;}
            f();
            count += 1;
        }
        $.maxStop(0); //restores it for actual use later.
        return ["while and recursion", count, 30, "this shows the same maxStop is being used"];
    }, 

]);

So enjoy and be careless! $.maxStop() has got your back.***

***not good to leave this in actual code, particularly for loops/recursions that could be huge. It should be an unnecessary function call in production code.

Post a Comment

Your email is never published nor shared.