Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 27, 2023 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/673687c55bccb2f492bcdf22d7e76c46 to your computer and use it in GitHub Desktop.
Save code-boxx/673687c55bccb2f492bcdf22d7e76c46 to your computer and use it in GitHub Desktop.
Ways To Abort Javascript

WAYS TO ABORT JAVASCRIPT

https://code-boxx.com/abort-javascript-execution/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

function test () {
// (A) DO YOUR STUFF AS USUAL
console.log("Test started");
let i = 0;
i++;
// (B) ABORT - RETURN "UNDEFINED"
if (i != 0) { return; }
// (C) RETURN RESULT IF OK
console.log("This part will not run");
return i;
}
// (D) FIRE TEST()
let i = test();
console.log(i);
// (E) BEST TO HANDLE THE ABORTION GRACEFULLY
if (i==undefined) {
// HANDLE ABORT
}
function test () {
// (A) DO YOUR STUFF AS USUAL
console.log("Test started");
let i = 0;
i++;
// (B) ABORT - THROW ERROR
if (i != 0) {
throw new Error("Manual Abort Script");
}
// (C) RETURN RESULT IF OK
console.log("This part will not run");
return i;
}
// (D) BEST TO TRY-CATCH FOR A GRACEFUL SCRIPT
try {
let x = test();
} catch (err) {
console.log(err);
}
// (A) SET TIMER & RUN
var timer = setInterval(
() => console.log("Running"), 3000
);
// (B) "FORCE" ABORT IMMEDIATELY
clearInterval(timer);
// (C) OR SET A "TIMEOUT"
setTimeout(() => {
clearInterval(timer);
console.log("Stopped");
}, 1000);
// (A) CREATE NEW WORKER
var theWorker = new Worker("4b-worker.js");
// (B) WHEN THE WORKER IS DONE
theWorker.onmessage = evt => {
console.log("Worker script has completed.");
console.log(evt.data);
};
// (C) SEND DUMMY DATA TO WORKER
// ! WORKER SCRIPT CANNOT ACCESS DOM DIRECTLY !
theWorker.postMessage({
Name : "John Doe",
Email : "john@doe.com"
});
// (D) ABORT AND TERMINATE WORKER
// theWorker.terminate();
// (A) START ONLY WHEN DATA RECEIVED
onmessage = evt => {
// (B) DATA RECEIVED
console.log("Worker has received data and started.");
console.log(evt.data);
// (C) DO PROCESSING HERE
// ...
// (D) REPLY RESULT
postMessage({
status: 0,
message: "OK"
});
};
// (A) DO YOUR STUFF
var first = 123;
var second = 456;
var third = first + second;
console.log(third);
// (B) STOP LOADING
window.stop();
console.log("This will still run...");
console.log("THIS WILL NOT RUN");
// THIS IS FOR NODEJS ONLY
// (A) DO YOUR STUFF
var first = 123;
var second = 456;
var third = first + second;
console.log(third);
// (B) ABORT OR EXIT
// ABORT = IMMEDIATE (ROUGH)
// EXIT = AS QUICKLY AS POSSIBLE (GRACEFUL)
// process.abort();
process.exit();
console.log("THIS WILL NOT RUN");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment