Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS Function closures for persistent memory #105

Open
dvas0004 opened this issue Oct 2, 2019 · 1 comment
Open

JS Function closures for persistent memory #105

dvas0004 opened this issue Oct 2, 2019 · 1 comment
Assignees

Comments

@dvas0004
Copy link
Owner

dvas0004 commented Oct 2, 2019

Javascript functions can return other functions, and the inner function has access to the scope of the outer function, making it useful to store variables persistently between function calls, for example:

var checkCookie = function() {
    var lastCookie = document.cookie; // 'static' memory between function calls
    return function() {
        var currentCookie = document.cookie;

        if (currentCookie != lastCookie) {
            // something useful like parse cookie, run a callback fn, etc.
            lastCookie = currentCookie; // store latest cookie
        }
    };
}();
window.setInterval(checkCookie, 100); // run every 100 ms
@dvas0004 dvas0004 self-assigned this Oct 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant