将脚本插入脚本

时间:2017-10-20 00:36:24

标签: javascript

我真的不明白如何将这两个代码放到一起。 我想把它捣碎给togheter所以它有效。

代码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>
&#13;
&#13;
&#13;

代码2:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

为什么不这样做?

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

正如@Slaks所指出的那样,您需要了解功能。现在,请看下面的代码。

<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>