1

I have this JS function:

<script type="text/javascript">
 var counter = <?php echo $oak; ?>;
 function myFunction() {
 counter++;
 document.getElementById('countervalue').innerHTML = counter;
 }
 </script>

This function is triggered once an image is clicked on. My problem is that I want it to only trigger the first time the image is pressed. Any ideas? Thanks in advance.

html code:

<div class="cut_oak_tree">
 <img src="http://www.pbpixels.com/x/images/oak.png" onclick="loadXMLDoc(this.outerHTML), myFunction()" /> <!--DO NOT CHANGE SRC -->
 <br>
 <img src="http://www.pbpixels.com/x/images/oak.png " onclick="loadXMLDoc(this.outerHTML), myFunction()" /> <!--DO NOT CHANGE SRC -->
 <img src="http://www.pbpixels.com/x/images/oak.png " onclick="loadXMLDoc(this.outerHTML), myFunction()" /> <!--DO NOT CHANGE SRC -->
 </div>
asked Feb 13, 2014 at 13:30
4
  • How you set this to be called on image click? Show us this code. Commented Feb 13, 2014 at 13:31
  • A boolean to keep track of whether it's been clicked? Remove the click handler from the image? Commented Feb 13, 2014 at 13:31
  • Edited html code and good idea^ Commented Feb 13, 2014 at 13:32
  • @user3287771 instead of removing the click handler you can also edit the function body to do nothing if the flag is true Commented Feb 13, 2014 at 13:33

6 Answers 6

2

Try this code:

Add this on the event call(onclick="myFunction(this)") then your function will looks like function myFunction(img) {. Then inside it remove its click handler:

img.onclick = null;

Final result is:

function myFunction(img) {
 counter++;
 document.getElementById('countervalue').innerHTML = counter;
 img.onclick = null;
}

And an image example:

<img src="http://www.pbpixels.com/x/images/oak.png " onclick="myFunction(this)" />

I wouldn't use a boolean like @SLoW suggested because that way you won't be able to know which image was already clicked. To do that you'll have to use an object and store each images id - or something like this - and its clicked state.

answered Feb 13, 2014 at 13:37
Sign up to request clarification or add additional context in comments.

4 Comments

Nice, it almost worked, my problem is that if you press the image fast enough, it will still count 1 up before the image is removed :o So basically you can get like 5 clicks out of the same tree
@user3287771 but there's nothing on your post talking about removing the image.. so I can't say nothing about it.
Try moving img.onclick.null above counter++ and see if that helps.
Thanks @SLoW that did the job and thanks DontVoteMeDown!
1

Add a flag

function myFunction() {
 if(!myFunction.invoked){
 counter++;
 document.getElementById('countervalue').innerHTML = counter;
 myFunction.invoked = true;
 }
}
myFunction.invoked = false;
answered Feb 13, 2014 at 13:35

Comments

1

Remove the onclick handler, by sending the element you clicked to the function:

<img src="http://www.pbpixels.com/x/images/oak.png " onclick="loadXMLDoc(this.outerHTML); myFunction(this)" />
function myFunction(elm) {
 elm.onclick = null; // Remove the onclick
 counter++;
 document.getElementById('countervalue').innerHTML = counter;
} 
answered Feb 13, 2014 at 13:39

Comments

1

if you are interested in a jquery approach you can use .one() ! http://api.jquery.com/one/

 var counter = <?php echo $oak;?>
 $(".cut_oak_tree img").one("click",function(){
 counter++;
 $("#countervalue").text(counter); 
 });
answered Feb 13, 2014 at 13:42

1 Comment

i hopelessly in love with jquery :P designm.ag/resources/…
1
// the wrapper that allows to call the inner function only once
function once(inner) {
 var executed = false;
 return function (args) {
 if (executed === false) {
 executed = true;
 inner.apply(this, arguments);
 }
 }
}
// a test function that should only be called once
var inner1 = function (name, age) {
 window.alert("1 " + name + " -> " + age);
}
// another test function
var inner2 = function (name, age) {
 window.alert("2 " + name + " -> " + age);
}
// testing
var wrapped1 = once(inner1);
wrapped1("Tom", 23);
wrapped1("Tina", 24);
wrapped1("Toby", 25);
var wrapped2 = once(inner2);
wrapped2("Tom", 23);
wrapped2("Tina", 24);
wrapped2("Toby", 25);
answered Feb 13, 2014 at 13:42

4 Comments

What exactly is unclear? Ask and I'll try to explain it more. The key point of this solution is, that with once you have outsource the problem of making functions only be executable once. It can be reused.
Looks like this is like the .one function from jQuery (api.jquery.com/one).
Yeah I have understood but I don't know, it was like using a bazooka to kill an ant.
Well, the only thing needed from my snippet is the topmost function. The rest is only for demonstration purposes. But yes, I would not write it myself, too, since I use underscore.js (which has such a wrapper function and much more).
0

I'd create a variable called isClicked, set it to false when the page is loaded, and then set it to true when the image is clicked. Then write an if statement to make sure isClicked is false before incrementing the counter.

<script type="text/javascript">
 var counter = <?php echo $oak; ?>;
 var isClicked = false;
 function myFunction() {
 if (isClicked === false){
 isClicked = true;
 counter++;
 }
 document.getElementById('countervalue').innerHTML = counter;
 }
</script>
answered Feb 13, 2014 at 13:32

2 Comments

Works perfect, only problem is that I have three images using the same function, it has to count 1 up every time an image is pressed. Any ideas? Thanks
I'm assuming you want to track each image's clicks separately? Going with the boolean approach, perhaps an array of booleans, each one tracking, or use @DontVoteMeDown's approach with passing the img as a parameter to the function and removing the click handler. That would seem to work.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.