简体   繁体   English

防止js文件被调用两次

[英]Preventing a js file from being called twice

Is there a way to put a variable in a javascript file so that it increments whenever the file is called (like a static var in Java)? 有没有一种方法可以将变量放入javascript文件中,以便在每次调用该文件时该变量都会递增(例如Java中的static var)? I want to use it to prevent calling a js file twice. 我想用它来防止两次调用js文件。 Or, is there another way to do this? 或者,还有另一种方法吗?

var checkit = window.check_var;

if(checkit === undefined){ //file never entered. the global var was not set.
    window.check_var = 1;
}
else {
    alert('FILE WAS CALLED ALREADY!');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM