[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

add +1 to a global variable from different functions

I need to keep a counter every time cretin functions are run and once the counter is at 3 another function is run.
0
gvilla23
Asked:
gvilla23
  • 4
  • 4
1 Solution
 
gvilla23Author Commented:
kind of like
var cnt = 1

function 1 {
cnt++
}

function 2 {
cnt++
}

function 3{
if cnt ==3
alert("hello")
}
0
 
Aaron TomoskyTechnology ConsultantCommented:
From what you describe wouldn't each of the functions need the line if cnt==3?
0
 
leakim971PluritechnicianCommented:
if cnt is really a global variable you don't need to add anything else
<script>
var cnt = 1

function 1 {
cnt++
alert(cnt)
}

function 2 {
cnt++
alert(cnt)
}

function 3{
if cnt ==3
alert("hello")
}
</script>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
leakim971PluritechnicianCommented:
good syntax :
<script>
var cnt = 1;

function f1() {
cnt++;
alert(cnt);
}

function f2(){
cnt++;
alert(cnt);
}

function f3(){
if(cnt ==3) {
alert("hello");
}
</script>

Open in new window

0
 
gvilla23Author Commented:
This is what I have so far, it keeps count but I can not get the 3rd function to catch

<script type='text/javascript'>
var cnt = 1;
$(document).ready(function(){

$('#div1').mouseover(function() {
alert(cnt)
cnt++
});

$('#div2').mouseover(function() {
alert(cnt)      
cnt++
});

$(function() {
if(cnt == 3) {

alert("hello")
}
});

});

</script>
</head>
<body>
<input  type='radio' id='div1' name='fields_fname' />
<br><br>
<input  type='radio' id='div2' name='fields_fname' />
</body>
</html>
0
 
leakim971PluritechnicianCommented:
what is the goal of :
<script type='text/javascript'>
var cnt = 1;
$(document).ready(function(){

$('#div1').mouseover(function() { 
alert(cnt)
cnt++
checkalert();
});

$('#div2').mouseover(function() { 
alert(cnt)      
cnt++
checkalert();
});

function checkalert() {
if(cnt == 3) {

alert("hello")
}
};

});

</script>
</head>
<body>
<input  type='radio' id='div1' name='fields_fname' />
<br><br>
<input  type='radio' id='div2' name='fields_fname' />
</body>
</html>

Open in new window

0
 
gvilla23Author Commented:
I wanted to keep tabs on how many times a function is run.

Thank you for you help leakim97, much appreciated.
0
 
gvilla23Author Commented:
Thanks.
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now