Wednesday, June 11, 2025
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy
T3llam
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
T3llam
No Result
View All Result
Home Services & Software

Totally different Methods to Abort JavaScript Execution

admin by admin
July 24, 2023
in Services & Software
0
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


Aborting JavaScript execution will help us to put in writing extra strong, safe, and environment friendly code by stopping surprising errors and behaviors.

Potential explanation why we have to abort JavaScript execution are:

  • We would have to abort execution when an error happens to stop additional execution of the code and keep away from surprising conduct or information corruption.
  • We would have to abort execution when person enter is invalid to stop the code from operating with incorrect or surprising enter.
  • We would have to abort execution in the midst of a loop if a particular situation is met, or if we’ve discovered the end result we had been in search of.
  • We would have to abort execution in some instances to optimize efficiency, comparable to when utilizing setInterval or setTimeout, the place we would have to cease the execution of a operate or loop after a particular period of time.
  • In some instances, it is perhaps essential to abort execution to stop malicious code from operating, or to stop delicate information from being accessed.

Alternative ways to abort JavaScript execution are as follows:

Utilizing the return assertion

Return statements in a programming language are used to skip the at the moment executing operate and return to the caller operate

Instance:

Javascript

operate checkNumber( num ) {

    if ( isNaN( num ) ) {

        return "Not a quantity!";

    }

    return num * 2;

}

  

console.log( checkNumber( "abc" ) );

console.log( checkNumber( 7 ) );

On this instance, the return assertion is used to abort the operate checkNumber if the enter will not be 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 change. 

Instance:

Javascript

let fruits = [ "banana", "apple", "orange", "grape", "strawberry" ];

  

for (let i = 0; i < fruits.size; i++) {

    if (fruits[i] === "orange") {

        break;

    }

    console.log(fruits[i]);

}

On this instance, the break assertion is used to exit the for loop when the ingredient “orange” is discovered within the fruits array. The break assertion can be utilized to exit a loop or change 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 required situation going to happen.

Instance:

Javascript

let numbers = [ 1, 2, 3, 4, 5 ];

  

for (let i = 0; i < numbers.size; i++) {

    if (numbers[i] % 2 === 0) {

        proceed;

    }

    console.log(numbers[i]);

}

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

operate checkAge( age ) {

    if ( age < 18 ) {

        throw "Have to be 18 or older";

    }

    console.log( "You're allowed to enter" );

}

  

strive {

    checkAge( 17 );

} catch ( error ) {

    console.log( error );

}

Output

Have to 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 sources within the present searching context, just like the browser’s cease button

Instance: We have now a webpage with some content material that takes a very long time to load, and also you need to give the person the power to cease the loading course of in the event that they get uninterested in ready. You may add a “Cease” button to your web page and fasten a click on occasion listener to it. Contained in the occasion listener operate, you possibly can name the window.cease() operate to right away cease the loading course of.

HTML

<!DOCTYPE html>

<html>

<head>

    <title>Cease Loading Instance</title>

</head>

  

<physique>

    <h1>Loading Instance</h1>

    <p>This web page is taking a very long time to load....</p>

    <button id="stop-btn">Cease Loading</button>

  

    <script>

        const stopBtn = doc.getElementById('stop-btn');

        stopBtn.addEventListener('click on', () => {

            window.cease();

        });

    </script>

</physique>

    

</html>

Within the above instance, when the person 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 bettering the person expertise on slow-loading pages.

The clearTimeout() and clearInterval strategies in javascript clear the timeout which has been set by the setTimeout() and setInterval features earlier than that.

Instance: 

Javascript

let timeoutId = setTimeout(operate () {

    console.log("This message will probably be logged after 3 seconds");

}, 3000);

  

clearTimeout(timeoutId);

  

let depend = 0;

let intervalId = setInterval(operate () {

    depend++;

    console.log("Rely is now:", depend);

}, 1000);

  

setTimeout(operate () {

    clearInterval(intervalId);

    console.log("Interval has been cleared");

}, 5000);

Output

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 will probably be logged to the console.

Subsequent, we use setInterval to execute a operate each second, which increments a depend 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 depend 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 will depend on the precise use case and what you need to obtain. It’s best to rigorously take into account 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

RelatedPosts

Diagram-based language streamlines optimization of advanced coordinated programs

Diagram-based language streamlines optimization of advanced coordinated programs

June 11, 2025
Newest Harness IDP replace higher helps developer portals at scale

Newest Harness IDP replace higher helps developer portals at scale

June 11, 2025
Home windows 11 is getting a nifty trick for rapidly mirroring your Android telephone proper from the Begin menu

Home windows 11 is getting a nifty trick for rapidly mirroring your Android telephone proper from the Begin menu

June 10, 2025
Previous Post

iPhone 14 Saves Man’s Life After He Plummeted 400 Ft Into Canyon

Next Post

Spotify Will increase Premium Plan Value to $10.99 a Month

Next Post

Spotify Will increase Premium Plan Value to $10.99 a Month

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • App (3,061)
  • Computing (4,398)
  • Gaming (9,595)
  • Home entertainment (633)
  • IOS (9,528)
  • Mobile (11,876)
  • Services & Software (4,003)
  • Tech (5,312)
  • Uncategorized (4)

Recent Posts

  • iOS 26 Will Let You Add Your U.S. Passport to Pockets for Id Verification
  • Redmi Watch Transfer evaluation – GSMArena.com information
  • Diagram-based language streamlines optimization of advanced coordinated programs
  • iOS 26 Contains These 100 New Options and Adjustments for Your iPhone
  • All Lies of P Overture DLC Bosses & Patterns
  • App
  • Computing
  • Gaming
  • Home entertainment
  • IOS
  • Mobile
  • Services & Software
  • Tech
  • Uncategorized
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. However you may visit Cookie Settings to provide a controlled consent.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analyticsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functionalThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessaryThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-othersThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performanceThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policyThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Save & Accept