admin管理员组文章数量:1400762
I have a function animate_images
running on some of my website's pages that performs a certain animation.
The problem I'm experiencing is that when a user navigates to the same page again, the function is called for the second time, in addition to its already running instance.
Is there a way to do something like this:
if ! (animate_images) {
animate_images();
}
Essentially, I want to make sure that animate_images
never runs more than once, no matter how many times it is actually called.
I have a function animate_images
running on some of my website's pages that performs a certain animation.
The problem I'm experiencing is that when a user navigates to the same page again, the function is called for the second time, in addition to its already running instance.
Is there a way to do something like this:
if ! (animate_images) {
animate_images();
}
Essentially, I want to make sure that animate_images
never runs more than once, no matter how many times it is actually called.
- 1 you could try using a global boolean – Peter Berg Commented Jun 18, 2013 at 16:38
- 2 How is the function still running when user loads another page or reloads the same one? – Dogbert Commented Jun 18, 2013 at 16:39
-
2
You could add to animate_images an
is_running
property, set ittrue
at the start of theanimate_images()
function and then set to tofalse
whenever the interval / timeout is cleared / the animation is stopped. – Paul Commented Jun 18, 2013 at 16:43 - 1 I like Paulpro's idea, but whatever you do, please do not use a global boolean. Once you start down that road you'll have your entire app running on global state sooner than you know it. – Sudhir Jonathan Commented Jun 18, 2013 at 16:49
-
1
Yes, if you know that the function will never be instantiated more than once (not called with
new
), then you can use a "static" property of your function object, as inanimate_images.is_running
. I can't remember offhand - that might technically makeanimate_images.is_running
a global (or in the same scope asis_running
), as opposed to actually being a property ofis_running
, but the effect should be the same. If you were instantiatinganimate_images
withnew
, then you would usethis
. IIRC in Javascriptthis
is scoped to the "closest" function instance. – taz Commented Jun 18, 2013 at 20:52
3 Answers
Reset to default 4Take a look at the once
function from underscore.js - linked here.
Essentially, you wrap the function in another one that has a flag variable inside it. Call the function if the flag is unset and set the flag when you call it.
_.once = function(func) {
var ran = false, memo;
return function() {
if (ran) return memo;
ran = true;
memo = func.apply(this, arguments);
func = null;
return memo;
};
};
If you don't want to add underscore to your website (I think that's the first thing you should do when starting any website, but I digress), do this:
runOnlyOnce = function(func) {...} // As defined above
animate_images = runOnlyOnce(animate_images);
or
animate_images = runOnlyOnce(function(){
alert('Animate all the things!');
});
The function itself has no dependencies on the rest of underscore.
Use a static property of your function object, as in a boolean animate_images.isRunning
. At the start of animate_images
, encapsulate the animation initialization with something like
animate_images() {
// on first load, animate_images.isRunning is undefined,
// so (!animate_images.isRunning) returns true.
if(!animate_images.isRunning) {
/* launch animation */;
// define animate_images.isRunning and assign true
animate_images.isRunning = true;
} else {
/* animation already initialized */
}
}
You could override your function with an empty function once it has been executed:
function a(){
console.log("hello");
a = function(){};
}
a(); //"hello"
a(); //>>nothing happens<<
Actually this will just override the reference a
to your function. So this only works if you don't reference to the same function multiple times. If you do something like this:
function a(){
console.log("hello");
a = function(){};
}
var obj { b:a };
a(); //"hello"
a(); //>>nothing happens<<
obj.b(); //"hello"
this method will fail.
本文标签: javascriptCall a function only if it hasn39t been calledrun a function only onceStack Overflow
版权声明:本文标题:javascript - Call a function only if it hasn't been calledrun a function only once - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744230609a2596313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论