10

I'm trying to call my class as the page loads, as well as well as reload the results ever X seconds, however following setTimeout tutorials, jquery seems to tossing me a error i don't understand considering it's syntaxlessness.

Uncaught RangeError: Maximum call stack size exceeded

var rand = function() {
    return Math.random().toString(36).substr(2);
};

lhc();

function lhc(){
    $('#lhcb a').each(function() {
        var rawlink = $(this).attr("href");
        var link = encodeURIComponent( rawlink );
        var token = rand();
        var href = $(this).prop('href');
        var proceed = $.getJSON( "lhc/link.php?link=" + link + "&a=c", function( data ) {
            if ( data.proceed == 'true' ) {
                return true;
            } else {
                return false;
            }
        }).error(function(e) { alert("error"); console.log(e);});
        if ( href.match("^javascript:") ) {
            proceed = false;
        }
        if ( rawlink.charAt(0) != '#' ) {
            if ( proceed ) {
                $(this).after( " <span style='font-size:xx-small;'>( Hits: <span id='" + token + "'></span> )</span>" );
                    $.getJSON( "lhc/link.php?link=" + link + "&a=q", function( data ) {
                        $('#' + token).html(data['hits']);
                    }).error(function(e) { alert("error"); console.log(e);});
                $(this).attr( "href", "lhc/link.php?link=" + link + "&a=g" );
            }
        }

    });
    setTimeout(lhc(), 5000);
}
WASasquatch
  • 1,044
  • 3
  • 11
  • 19
  • 2
    This is not a duplicate. The problem here is invalid call to `lhc()`, which is actually a simple syntax error. It causes the same message, but the origin is quite specific. – Dariusz Sep 18 '14 at 07:09

2 Answers2

26

Change

setTimeout(lhc(), 5000);

to

setTimeout(lhc, 5000);

You're calling the function directly without a timeout when you add the parenthesis, and calling the function right away inside the same function quickly becomes an endless loop that fills up the stack

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Thank you, that makes sense. Now I just gotta figure out my token issue and reusing tokens to update instead of it dropping in a whole new lol oops – WASasquatch Mar 29 '14 at 19:29
  • @adeneo Thanks and +1. You saved me :) but a question, you mean i can't send parameter to function when i want to set timeout for it. For now and using this approach i changed parameter to global variable but it is not so cute and good. Do you now how I could set timeout for a function and send parameter inside it. – QMaster Nov 08 '14 at 23:34
  • @QMaster closure - `setTimeout(function(){ lhc(param1, param2) }, 5000)` – daviestar Nov 13 '14 at 00:49
  • 1
    @QMaster or .bind - setTimeout(lhc.bind(null, param1, param2), 5000) – Dg Jacquard Nov 18 '14 at 15:32
0

You can to ignore error if running <script>window.onerror = function(){ return true;} </script> And set setTimeout(lhc, 5000); some.

Rogerio de Moraes
  • 1,527
  • 18
  • 15