-2

I guess it is not possible, but I just wanted to know if it could be possible to have an if statement always "active". I am creating a text-based game and the health variable of the character is changing often, so when it reaches 0 or below, i want to execute a "dead" function.

if (health < 1) {
  //code
};

Do I have to include that if-statement everytime in the object, when the health variable changes?

Thank you a lot :)

Robstaa
  • 33
  • 5
  • Is `health` text within a node of an `html` document? – guest271314 Sep 06 '16 at 03:09
  • Use a [setter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) if the `health` is a member of an object (which it should be), and then put the `if` statement inside the setter. – 4castle Sep 06 '16 at 03:10
  • a while statement,... – Ryan McCullagh Sep 06 '16 at 03:10
  • use a while loop, instead of if – Grainier Sep 06 '16 at 03:11
  • 3
    Don't use a while loop, that would freeze up the UI until you exit the loop – Patrick Evans Sep 06 '16 at 03:13
  • OP, with the limited info given, I assume you have the text in some HTML element. you should look into Javascript's onChange event handler. You can use object.onchange=function(){ //check if health is less than 1. }; – Thumper Sep 06 '16 at 03:18
  • You can have a function which is on a timeout, or interval (`setTimeout()` or `setInterval()`) which will perform the `if` after a particular time, or at specified intervals. – Makyen Sep 06 '16 at 03:19
  • Another possibility would be that the check could be part of a setter within the Object so the check is automatically done each time the value is changed. What is going to work well is going to depend on what you are doing in your code. You have not supplied enough information for us to give good answers here. – Makyen Sep 06 '16 at 03:21
  • Hey guys, thanks for the input. A loop is not possible, as I have explained below. @Thumper, I tried the onchange, but it does not seem to execute, when health is below 1. I will put the code below: **JS:** window.onload = function() { document.getElementById("health").onchange = function() { if (health < 1) { alert("dead"); }; }; }; the var health is changed by this function: function() { health = health - 10; document.getElementById("health").innerHTML = health; } Thanks for your help :) – Robstaa Sep 07 '16 at 15:49
  • Can you post a working code snippet? I have some ideas, but we need to see how you're changing things. Here is a modified version of the code you provided. – Thumper Sep 08 '16 at 03:05
  • – Thumper Sep 08 '16 at 03:05
  • did it now like this, easier than expected, but works, even though it might not be considered as beautiful code :p @Thumper - thanks for your help! – Robstaa Sep 09 '16 at 18:21
  • function getHealth(healthUp, healthDown) { this.healthUp = healthUp; this.healthDown = healthDown; health = health + healthUp - healthDown; document.getElementById("health").innerHTML = money; if (health < 1) { console.log("oom"); }; } – Robstaa Sep 09 '16 at 18:22
  • BTW, @PatrickEvans, this is not a duplicate of the provided dupe - this is a different problem than that; OP should clarify this is about event listening, in general. – Thumper Sep 12 '16 at 18:09

1 Answers1

0

If statement is simple decision if you want to iterate than you can write a loop for this purpose as below:

while(health > 1){
   //Decrease health
}
//now call dead function
  • Without more explanation, this could easily lead to the OP writing code locking up the CPU in a loop which will never exit due to health not changing. For example, by expecting health to be changed in some asynchronous manner, like from user input. – Makyen Sep 06 '16 at 03:16
  • As defined in the question that health variable is continuous changing . So it will not locking up the CPU in a loop – Zeeshan Afzal Satti Sep 06 '16 at 03:20
  • It is "changing often", but we don't know how it is changing or what is causing it. If what is causing the change is an asynchronous event (e.g. user input), then having a loop that just camps waiting for a value to change will lock up the CPU and can prevent the event that causes the change from affecting the variable being tested. Potentially this ends up resulting in never exiting the loop while CPU load is max. – Makyen Sep 06 '16 at 03:25
  • Thanks a lot for that idea, but @Makyen is right. The user is changing the variable by actions he is pursuing. And those actions are not inside the if or while function, but outside of it. Also the loop doesnT exit necessarily, e.g. if the user does not die (does not decrease health below 1). I tried it out and the CPU froze, but thanks a lot for your input! – Robstaa Sep 07 '16 at 15:11