javascript - Cache a variable exclusively on a function -
is there way cache global variable function within group of functions, without calling function directly another?
for example, if have group of functions wrapped in parent function this:
function parentfunction() { var myvariable; somediv.addeventlistener('click', function (e) { myvariable = e.target; }); anotherdiv.addeventlistener('click', function (e) { // use myvariable without changing when above fired again. }); }
the global variable declared @ start, given value in first function, carries on second use.
but how can stop continually updating in second function, if first function fires again?
could add event-listener inside second function check if first fires again , ensure variable doesn't change?
you can set variable once in first function:
somediv.addeventlistener('click', function (e) { if (!clickedlink) { clickedlink = e.target; } });
or, can apply logic want there. sometimes, saving state in semi-global later use in different event handler warning sign might have design issue. if explain more you're trying do, offer opinion on whether there's better way solve design issue.
Comments
Post a Comment