Javascript 如何检测使用javascript调用的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3332265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to detect a function was called with javascript
提问by Chameron
I have a javascript function.
我有一个 javascript 函数。
How to check:
如何检查:
if function was called ( in
<head></head>section have this function), then not to call the functionif function was not called ( in
<head></head>section haven't this function), then call the function
如果函数被调用(在
<head></head>节中有这个函数),则不调用该函数如果没有调用函数(在
<head></head>节中没有这个函数),则调用该函数
like require_onceor include_oncewith PHP
喜欢require_once或include_once使用PHP
回答by Juan Mendes
Static variables
静态变量
Here's how to create static (like in C) variables using self calling functions to store your static variables in a closure.
以下是如何使用自调用函数创建静态(如在 C 中)变量以将静态变量存储在闭包中。
var myFun = (function() {
var called = false;
return function() {
if (!called) {
console.log("I've been called");
called = true;
}
}
})()
Abstract the idea
抽象的想法
Here's a function that returns a function that only gets called once, this way we don't have to worry about adding boiler plate code to every function.
这是一个返回一个只被调用一次的函数的函数,这样我们就不必担心向每个函数添加样板代码。
function makeSingleCallFun(fun) {
var called = false;
return function() {
if (!called) {
called = true;
return fun.apply(this, arguments);
}
}
}
var myFun = makeSingleCallFun(function() {
console.log("I've been called");
});
myFun(); // logs I've been called
myFun(); // Does nothing
回答by viam0Zah
Use decorator pattern.
使用装饰器模式。
// your function definition
function yourFunction() {}
// decorator
function callItOnce(fn) {
var called = false;
return function() {
if (!called) {
called = true;
return fn();
}
return;
}
}
yourFunction(); // it runs
yourFunction(); // it runs
yourFunction = callItOnce(yourFunction);
yourFunction(); // it runs
yourFunction(); // null
This solution provides a side-effect free way for achieving your goal. You don't have to modify your original function. It works nice even with library functions. You may assign a new name to the decorated function to preserve the original function.
此解决方案提供了一种无副作用的方式来实现您的目标。您不必修改原始函数。即使使用库函数,它也能很好地工作。您可以为装饰函数指定一个新名称以保留原始函数。
var myLibraryFunction = callItOnce(libraryFunction);
myLibraryFunction(); // it runs
myLibraryFunction(); // null
libraryFunction(); // it runs
回答by Sean Patrick Floyd
You can use a global variable in a custom namespace to store whether the function has been called.
您可以在自定义命名空间中使用全局变量来存储函数是否已被调用。
if(!window.mynamespace){
window.mynamespace={};
}
mynamespace.callMeOnlyOnce=function(){
if(mynamespace.alreadyCalled)return;
alert('calling for the first time');
mynamespace.alreadyCalled=true;
};
// alert box comes
mynamespace.callMeOnlyOnce();
// no alert box
mynamespace.callMeOnlyOnce();
回答by meder omuraliev
var called = false;
function blah() {
called = true;
}
if ( !called ) {
blah();
}
回答by Thevs
If (!your_func.called) {
your_func.called = true;
your_func();
}

