Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: Can't find variable error

I have this code:

$(document).ready(function(){
    var callPage = function(){
        $.post('/pageToCall.php');
    };

    setInterval('callPage()', 60000);
});

and it gives me the error ReferenceError: Can't find variable: callPage. Why?

like image 661
Alex Avatar asked Dec 16 '22 10:12

Alex


1 Answers

Try setInterval(callPage, 60000);.

If you pass a string to setInterval, then this string is evaluated in global scope. The problem is that callPage is local to the ready callback, it is not global.

There is hardly ever a reason to pass a string to setInterval (setTimeout). Always pass a function (to avoid exactly this kind of errors).

like image 114
2 revs, 2 users 71% Avatar answered Dec 30 '22 15:12

2 revs, 2 users 71%