I’d like to build a function that returns false if it’s been called less that half a second ago.
timething.timechill=function(){
var last
if (last){
if ((now.getTime()-last)>500){
return true
}
else{
return true
}
}
else {
last=now.getTime()
return false
}}
Any ideas? I’d like to avoid setTimeout() and ignore input if it’s coming too quick to avoid overflow. Is this a good practice?
The idea here is that
(function() { ... })();will create an anonymous function and run it immediately.timething.timechillis not assigned this function. Instead, it is assigned the inner function returned by this function.Note that
lastCallis not declared (using thevarkeyword) within that inner function. And when the outer function returns,lastCalldoesn’t disappear because the inner function has “enclosed” it by virtue of the fact that it refers to the variable.When you run
timething.timechilllater and it encounters this variable, it will search outside the function’s scope for the variable and find the one that was declared earlier. When it returns, the variable still does not disappear, since it was declared outside the function’s scope.It is hard to explain this concept clearly, but it is very useful because
lastCallis invisible to the rest of your code which doesn’t need to see it.