如果在javaScript中有条件就可以运行! [英] do function if condition in javaScript !

查看:84
本文介绍了如果在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);
}


这篇关于如果在javaScript中有条件就可以运行!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆