Laurentiu Costache
Laurentiu Costache

Reputation: 5

JavaScript & Cookies

I have an issue with a js (i use jquery cookie). I want to display a div after a few seconds. and when it should display in firbug i get an error that says:

ReferenceError: showIt is not defined
setTimeout('showIt()', 60000); // after 8 min

And this is my coode for cookie.js

$(function() {  
    function showIt() {
        $('#buy').show();
    }

    //cookie
    if ($.cookie('sls') == 'true') {    
        showIt();
    } else {
        setTimeout('showIt()', 60000); // after 8 min
    }

    $.cookie('sls', 'true');
    });

Thanks for your support

Upvotes: 0

Views: 46

Answers (1)

Barmar
Barmar

Reputation: 782206

Change to:

setTimeout(showIt, 60000);

When you give a string argument to setTimeout, the Javascript is evaluated in the global scope. But your function is defined locally to the anonymous function, so it's not in the global scope. Pass the function itself rather than a string and it will be evaluated in the local scope.

Upvotes: 2

Related Questions