开发者

Interrupt running code

开发者 https://www.devze.com 2023-03-29 05:43 出处:网络
For example; They are my links,HomePage开发者_如何学C - Users When I click homepage load js code. How I can interrupt when click Users this running code and start again.

For example; They are my links, HomePage开发者_如何学C - Users

When I click homepage load js code. How I can interrupt when click Users this running code and start again.

Thanks for answers.


First of all, your individual Javascript functions should only be running for split seconds. There should be no single long-running function that you need to actually interrupt. If there is, you need to design it so it is interruptable. Like:

function foo() {
    while (/* something */) {
        if (stopFlag) {
            return;
        }

        // do something
    }
}

When you want to interrupt the function, you set the global stopFlag to true and the function will exit soonish. That's rarely a necessary design pattern in the browser though (and only possible to a limited extend in the first place due to the single-threadedness of JS).

The best answer I can give to this vague and broad question of yours: design your Javascript in a way that you don't have this problem in the first place.


$('.element_class').click( function(event){
// your code here
event.preventDefault();
});

or

$('.element_class').click( function(event){
// your code here
return false;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消