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>
-
How you set this to be called on image click? Show us this code.DontVoteMeDown– DontVoteMeDown2014年02月13日 13:31:09 +00:00Commented Feb 13, 2014 at 13:31
-
A boolean to keep track of whether it's been clicked? Remove the click handler from the image?Teepeemm– Teepeemm2014年02月13日 13:31:58 +00:00Commented Feb 13, 2014 at 13:31
-
Edited html code and good idea^user3287771– user32877712014年02月13日 13:32:46 +00:00Commented 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 trueIvaylo Strandjev– Ivaylo Strandjev2014年02月13日 13:33:31 +00:00Commented Feb 13, 2014 at 13:33
6 Answers 6
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.
4 Comments
Add a flag
function myFunction() {
if(!myFunction.invoked){
counter++;
document.getElementById('countervalue').innerHTML = counter;
myFunction.invoked = true;
}
}
myFunction.invoked = false;
Comments
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;
}
Comments
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);
});
1 Comment
// 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);
4 Comments
once you have outsource the problem of making functions only be executable once. It can be reused..one function from jQuery (api.jquery.com/one).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>