• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • 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 TomoskySD-WAN SimplifiedCommented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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