如果在javaScript中有条件就可以运行!

如果在javaScript中有条件就可以运行!

问题描述:

我在javaScript中有两个函数!

喜欢这些:



I have two function in javaScript !
like these :

function first()
{
var a= 10;
}

function second()
{
//when a=10 run this function
}





我有一个按钮就可以了:



and i have a button loke this:

<button>Button</button>





现在我的问题:

我想要第二个功能延迟,当a = 10运行时!

我怎么能这样做?



now my problem :
I want the second function delay and when a=10 run it !
How i can do this ?

每次发生更改时都会调用第一个函数,然后检查值一个。这样的东西,



You would be calling the first function each time the change occurs, and then check against the value of a. Something like this,

function first()
{
  var a=10;
  // value will always be 10 if you assign it like above
  if(a == 10) {
    // trigger the second function
    second();
  }
}





..要点击按钮执行第一个功能,你可以写 onclick HTML中的事件,





.. to execute the first function on the button click, you can write the onclick event in the HTML as,

<button onclick="first()">Button</button>





您知道吗,您可以直接将HTML标记中的值作为参数传递给JavaScript吗?





Did you know, you can directly pass the value from HTML markup to the JavaScript as a parameter too?

<button onclick="first(10)">Button</button>







// a is the parameter; with 10 as value
function first(a) {
   // this is the body of the function..
   if(a == 10) {
      // execute the second function...
      second();
   }
}


从你的简短问题我明白你想要一个定时器延迟?或者是'一个'输入?这是一个定时的延迟,其中10 =秒这样做



from your brief question i understand you want a timer delay? or is 'a' an input? its its a timed delay where 10 = seconds do like this

function first(){

    var second = function(){
    alert("hello world");
    }
    //will start a timer and then run second() after 10secs
    window.setTimeout(second, 10000);
}