2

This time, I've been doing research over how to stop executing JS app. Unfortunately, I didn't find any matching my needs code... Anyway, I want to write function, that stops my app, after typing close.After that, main app won't recognize child app's commands, unless you load that child app again.Here's my code:

var command = $("input[type='text']").val();

if(command === "close"){
   // Close this app
}

FULL VERSION HERE: todo-list on Github

  • reload the page: window.location.reload(true); – Alex S. Jun 17 '18 at 10:47
  • @AlexS. That's not what I want to do, I just want to stop one JS app. –  Jun 17 '18 at 11:04
  • break, will help you if you want to stop loop – manan5439 Jun 17 '18 at 13:00
  • @manan5439 There's no loop in my app. I'll edit description, so that everyone can have a look at full version of code, if neccesarry. –  Jun 17 '18 at 13:01
  • 1
    There's no such thing as "closing an app" in the browser; either the browser window is open and running, or the window is closed. If you want to remove a portion of your script that is attached to a particular dom node, remove that dom node or unbind any event triggers that fire the unwanted code. – Daniel Beck Jun 17 '18 at 13:19
  • @DanielBeck Hmm... that's good point to start with. –  Jun 17 '18 at 13:21

3 Answers3

0

If you mean never execute anything in the current page you could use a global variable.

stopExec = false;
var command = $("input[type='text']").val();

if(command === "close"){
   // Close this app
   stopExec = true;
}

With this now you will have to check in all function the state of stopExec like this

function foo(){
    if(stopExec === false){
        // code of foo()
    }
}
Core972
  • 3,715
  • 3
  • 28
  • 39
0

You have set stopExec to true in closeApp, which never called. Check my modification to your code.

// Add this line
stopExec = true;
else if(command === "close" && stopExec === false){
    closeApp(); 
}

// and in closeApp, you don't need to set stopExec (it wasn't executing before anyway)
    function closeApp(){
     printOut("APP WAS CLOSED", "success")
     stopExec = true
}

And, "command" is undefined. You will need to bind "command" to the value of the input when pressing enter like: event.target.value === "close".

yotke
  • 1,015
  • 2
  • 12
  • 24
  • So, I have very simple todo-list app, that is a part of bigger app. Basically, todo-list app has a command _close_, that is supposed to; surprise surprise, close child app. After that, main app won't recognize child app's commands, unless you load that child app again. –  Jun 17 '18 at 12:53
  • In this context, closing the child app is removing the top level dom node. Don't forget to unbind any event listeners attached to the removed dom node. – yotke Jun 17 '18 at 15:19
  • I'm doing this, and works just fine, but other, unrelated to this problem glitches occured. Still, I can consider it [problem with closing child app] solved. –  Jun 17 '18 at 18:16
  • Forgot to edit, command is defined in main app `var command = $("input[type='text']").val();` –  Jun 18 '18 at 07:20
  • Furthermore, this main app isn't avalible anywhere yet, as I'm still working on it. You'll be able to see it on 12th July... –  Jun 18 '18 at 07:22
0

maybe you have to improve you code with

var command = $("input[type='text']").val();

if(command === "close"){
process.exit()
}

here is a link link for more info

 process.exit(1) //return terminate with failure code
 process.exit(0) //return terminate with success  code 
manan5439
  • 808
  • 6
  • 23
  • you are using node js because it is only work with nodejs https://nodejs.org/api/ – manan5439 Jun 17 '18 at 13:13
  • Unfortunately, I get an error: `Uncaught ReferenceError: process is not defined` –  Jun 17 '18 at 13:16
  • I'm telling you, I don't need node.js here! –  Jun 17 '18 at 13:18
  • I think he assumed you were using node because that's the only js context in which "closing an app" (as opposed to just closing the browser window) makes sense. – Daniel Beck Jun 17 '18 at 13:22