Click here to Skip to main content
15,886,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two function in javaScript !
like these :

JavaScript
function first()
{
var a= 10;
}

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


and i have a button loke this:
HTML
<button>Button</button>


now my problem :
I want the second function delay and when a=10 run it !
How i can do this ?
Posted
Updated 22-Nov-14 22:12pm
v2

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

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


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

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


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

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


JavaScript
// 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();
   }
}
 
Share this answer
 
Comments
‫محم د‬‎ 23-Nov-14 4:23am    
in my project a has delay !
i want run second function wen user set Allow to user media !
Afzaal Ahmad Zeeshan 23-Nov-14 4:31am    
What sort of delay, you don't seem to show any code that would have a delay.

Where does the Allow media thing happens?
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

JavaScript
function first(){

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900