-2

I really do not understand how to put this 2 codes togheter. I want to mash this to togheter so it works.

Code 1:

<script type="text/javascript">

function myfunction(obj){
    if (obj.checked)
    {
        alert("checked");
        /// I want to put code 2 here
    }
    else
    {
        alert("Unchecked");
       /// I want to put code 2 here
    }
}
</script>

Code 2:

<script>
window.setInterval(function() {  
    httpGetAsync('test.php', function(text) {  
        if (document.getElementById("text").innerHTML >= 700) { 
 window.open("www.site.kom", "_blank"); 
 alert("Test 2");
        } 

    });  
}, 7000);
</script>
Dan O
  • 6,022
  • 2
  • 32
  • 50
ANdy
  • 13
  • 6
  • 1
    Learn about functions. – SLaks Oct 20 '17 at 00:40
  • Why do you want to put the same code in both branches? That doesn't make much sense. How/when is `myfunction` called? Do you really want to start a new interval every time the function is called? *"I want to mash this to togheter so it works."* You can simply copy and paste the second code in the places you want it to be. However, that might not do what you want to do (it might not "work"). But since you haven't explained what you want the code to do we cannot really help you. – Felix Kling Oct 20 '17 at 00:40
  • myfunction is called on a click. So i need code 2 to be loaded exactly when click is done. This is just an easy solution i think would work for me. – ANdy Oct 20 '17 at 00:42
  • So every time a click occurs you want to start a new interval? What's the point of that? Either way, I recommend to read http://eloquentjavascript.net/ to learn the basics. – Felix Kling Oct 20 '17 at 00:44
  • Now i know what you mean. Need a fix so it can be started and stopped. – ANdy Oct 20 '17 at 01:19

2 Answers2

1

Why not just do this?

<script type="text/javascript">

    function myfunction(obj){
        if (obj.checked)
        {
            alert("checked");
            otherFunction()
            /// I want to put code 2 here
        }
        else
        {
            alert("Unchecked");
            otherFunction()
           /// I want to put code 2 here
        }
    }
    function otherFunction() {
      window.setInterval(function() {  
            httpGetAsync('test.php', function(text) {  
               if (document.getElementById("text").innerHTML >= 700) { 
            window.open("www.site.kom", "_blank"); 
                alert("Test 2");
               } 
            });  
      }, 7000);
     }
</script>
Kartik Prasad
  • 730
  • 7
  • 18
0

As @Slaks pointed out, you'll need to learn about functions. For now, look at this code below.

<script type="text/javascript">
  function mySecondFunction() {
    window.setInterval(function() {  
      httpGetAsync('test.php', function(text) {  
          if (document.getElementById("text").innerHTML >= 700) { 
    window.open("www.site.kom", "_blank"); 
    alert("Test 2");
          } 

      });  
    }, 7000);
  }

  function myfunction(obj){
      if (obj.checked)
      {
          alert("checked");
          mySecondFunction();
      }
      else
      {
          alert("Unchecked");
        mySecondFunction();
      }
  }
</script>
Jason
  • 514
  • 5
  • 21
  • Worked but how do i stop the script? I want to stop ''otherFunction'' when Unchecked. – ANdy Oct 20 '17 at 01:18