I'm currently using jquery to run code once my page has loaded:
$(window).load(function(){
\\ stuff
};
How do you write jquery to run only if the page has already loaded and is redirecting, but the new website has not loaded yet? Any suggestions?
答案 0 :(得分:1)
You can begin executing code immediately:
<script type="text/javascript">
runSomeCode();
</script>
If you want it to stop running at a given event, then presumably what you have is some asynchronous operation which will repeat while the page loads. Something like this:
<script type="text/javascript">
setInterval(someFunction, someTimeout);
</script>
You can stop the operation when the event occurs:
<script type="text/javascript">
var operation = setInterval(someFunction, someTimeout);
$(window).load(function(){
clearInterval(operation);
};
</script>
However the operation is defined or constructed, it would have to be asynchronous in order for the page to continue to load. And it would have to be a loop in order for it to be "stopped". (Or at the very least some sequence of operations which checks a condition before executing each step.)
So basically you would start the operation based on some flag (in the above case the operation
variable, but in other structures perhaps just a simple boolean value), check for the flag within the operation's loop, and cancel the flag in the event handler.