I have recently started working in jQuery and now when i am working on an project where i am using ajax function and loading the same library(i have written) again and again. and due to that $(document).ready(function(){})
calls again and again. Means one function calls for 4 time if i use to load it 4 times again and again.
I want to clean/unready/unload previous ready function and want to ready new function . i use to try on $(function(){})
also. but the same result. i tried to get any respective/ related topic. but couldn't find out.
If i could not find this or no one can help me. then i think i should have to sp开发者_如何学运维lit/break my one library to numbers of libraries.
That works as intended, multiple event registrations will fire independently of each other.
You could make a workaround, though: set a flag that will prevent execution if called more than once. For example, this page would load your script four times, but the event registration would only happen once:
this is your HTML page
<script>
var alreadyRan = false;
</script>
<script src="yourscript.js?firstload"></script>
<script src="yourscript.js?secondload"></script>
<script src="yourscript.js?thirdload"></script>
<script src="yourscript.js?nextload"></script>
etc.
Then, in yourscript.js:
if (!alreadyRan) { // we declared alreadyRan in the global scope
alreadyRan = true; // won't run on further invocations
// thus the handler will only be registered one time
$(document).ready(function(){
// your onready code
});
}
Note that it might be easier to split your library into the part that is only needed once and into the part that is called repeatedly, and only load that initial part once.
精彩评论