Execute .click(function () only first click
Asked Answered
U

6

7

I trying to Execute given .click(function() only on first click, but it always Execute when click.

It's works when click .more-post-button it will add class loading to .main ul and show-more-post to .main li after that remove Class loading on setTimeout function 7 second.

JS:

$(".more-post-button").click(function () {

  $('.main ul').addClass('loading');
  $('.main li').addClass('show-more-post');
  setTimeout(function(){
    $('.main ul').removeClass('loading');
  },7000);

});

My question is how to do this for only on first click, not every time click.
Thanks in advance.

Upali answered 29/7, 2015 at 13:41 Comment(5)
after 7 second when class loading will removed should it work again?Celebes
Don't want .click(function() every time, want to first time only.Upali
api.jquery.com/oneCelebes
jsfiddle.net/3why3vLt/1Celebes
check this "#12886160"Wauters
U
9

Try:

$(".more-post-button").one("click", function () {

  $('.main ul').addClass('loading');
  $('.main li').addClass('show-more-post');
  setTimeout(function(){
    $('.main ul').removeClass('loading');
  },7000);
});

http://api.jquery.com/one/

It'll handle it only once.

Udine answered 29/7, 2015 at 13:48 Comment(0)
E
8

Why complicating things, try a simple closure on that. You won't pollute globale name space, too! Demo goes here

(function(){
'use-strict';

var button = document.getElementsByTagName('button')[0];
var myHandler = function() {
    var click = 0;
    return function() {
        if(click === 0) {
            alert('single click');
        }
        click++;
    }
}();


button.addEventListener('click', myHandler, false);
})();
Evalyn answered 29/7, 2015 at 13:55 Comment(2)
That a) still leaves an unused handler connected and b) is much longer code and c) is actually more complicated, not less :) Just use one as this is a jQuery question.Folse
Javascript is all about closures. What makes you think it's complicated? The question is tagged by javascript as well, jQuery is just a framework to it.Evalyn
G
3

Actually you can send in the addEventListener an option { once: true } to tell it to fire only once. So adding up to @fubbe and also using only javascript, it would be:

var button = document.getElementsByTagName('button')[0];
var handler = function () { alert('once click'); };

button.addEventListener("click", handler, {once: true});

Source :

Georgiegeorgina answered 14/2, 2018 at 21:3 Comment(0)
U
2

I would use jQuery's .one() function. This attaches a handler that will only fire once.

Modified JS

$(".more-post-button").one("click", function () {

  $('.main ul').addClass('loading');
  $('.main li').addClass('show-more-post');
  setTimeout(function(){
    $('.main ul').removeClass('loading');
  },1000);

});

Fiddle

Unscratched answered 29/7, 2015 at 13:49 Comment(0)
T
1

save it in a variable:

var clicked = false;

  $(".more-post-button").click(function () {
  if(!clicked) {
  clicked = true;
  $('.main ul').addClass('loading');
  $('.main li').addClass('show-more-post');
  setTimeout(function(){
    $('.main ul').removeClass('loading');
  },7000);
}
});
Topmost answered 29/7, 2015 at 13:42 Comment(1)
this wouldn't work. The .click is a bind, so it'll happen everytime. you need to put the flag check inside the click functionDodge
U
1

Use unbind in your function to remove all events

$(this).unbind();

From jQuery 1.7 .off is the recommended way

$(this).off();
Unexceptional answered 29/7, 2015 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.