Aborting JavaScript execution will help us to write down extra sturdy, safe, and environment friendly code by stopping surprising errors and behaviors.
Potential the reason why we have to abort JavaScript execution are:
- We’d must abort execution when an error happens to stop additional execution of the code and keep away from surprising habits or information corruption.
- We’d must abort execution when consumer enter is invalid to stop the code from working with incorrect or surprising enter.
- We’d must abort execution in the midst of a loop if a particular situation is met, or if we have now discovered the consequence we have been in search of.
- We’d must abort execution in some instances to optimize efficiency, similar to when utilizing setInterval or setTimeout, the place we would must cease the execution of a operate or loop after a particular period of time.
- In some instances, it is likely to be essential to abort execution to stop malicious code from working, or to stop delicate information from being accessed.
Other ways to abort JavaScript execution are as follows:
Utilizing the return assertion
Return statements in a programming language are used to skip the presently executing operate and return to the caller operate
Instance:
Javascript
|
|
On this instance, the return assertion is used to abort the operate checkNumber if the enter just isn’t a quantity, and return the message “Not a quantity!”.
The break Assertion comes out of the loop when the situation is true. It breaks out of the loop or swap.
Instance:
Javascript
|
|
On this instance, the break assertion is used to exit the for loop when the component “orange” is discovered within the fruits array. The break assertion can be utilized to exit a loop or swap assertion and cease the execution of the script.
The proceed assertion in Javascript is used to interrupt the iteration of the loop and follows with the subsequent iteration. The break within the iteration is feasible solely when the desired situation going to happen.
Instance:
Javascript
|
|
On this instance, the proceed assertion is used to skip the even numbers and print solely the odd numbers within the numbers array. The proceed assertion can be utilized to skip to the subsequent iteration of a loop and abort the present iteration.
Utilizing the throw assertion
The throw assertion enables you to make your personal errors
Instance:
Javascript
|
|
Should be 18 or older
On this instance, the throw assertion is used to throw an error and cease the execution of the operate checkAge if the enter age is lower than 18.
Utilizing the window.cease Technique
The cease() methodology in DOM is used to cease the window from loading assets within the present shopping context, just like the browser’s cease button
Instance: We now have a webpage with some content material that takes a very long time to load, and also you need to give the consumer the power to cease the loading course of in the event that they get bored with ready. You’ll be able to add a “Cease” button to your web page and fix a click on occasion listener to it. Contained in the occasion listener operate, you may name the window.cease() operate to right away cease the loading course of.
HTML
|
|
Within the above instance, when the consumer clicks the “Cease Loading” button, the window.cease() operate is named, which instantly stops the loading course of and shows no matter content material has already been loaded on the web page. This may be helpful for enhancing the consumer expertise on slow-loading pages.
The clearTimeout() and clearInterval strategies in javascript clear the timeout which has been set by the setTimeout() and setInterval capabilities earlier than that.
Instance:
Javascript
|
|
Rely is now: 1 Rely is now: 2 Rely is now: 3 Rely is now: 4 Interval has been cleared
Within the above instance, we first use setTimeout to execute a operate after 3 seconds. Nevertheless, we instantly cancel the timeout utilizing clearTimeout, so no message shall be logged to the console.
Subsequent, we use setInterval to execute a operate each second, which increments a rely variable and logs its worth to the console. After 5 seconds have handed, we use clearInterval to cancel the interval and log a message indicating that the interval has been cleared. In consequence, we see the rely enhance to five earlier than the interval is cleared, after which the message “Interval has been cleared” is logged to the console.
Conclusion: Thus, selecting the suitable methodology to abort JavaScript execution relies on the particular use case and what you need to obtain. You must rigorously think about the implications of every methodology and select the one that most closely fits your wants.
Final Up to date :
24 Jul, 2023
Like Article
Save Article
