2

I am having the image.When Image is clicked I show the data with some animation.So what I want to do is disable the image upto data shown in browser.Then once data is loaded I need to enable the image.

for example

$("#imageid").click(function(e){
//disable the click
$("idforanim").animate({left : "-=50 "px"},500,function(){callfunction1();})
 })

callfunction1(){
//show the data
//enable the click
}

How can I achieve this?

3 Answers 3

5

Another way would be to set a flag to indicate that the data is still being processed and unset the flag on completion:

var processing = false;

$("#imageid").click(function(e){
    if ( !processing )
    {
        //disable the click
        processing = true;
        $("idforanim").animate({left : "-=50px"},500,function(){callfunction1();})
    }
})

callfunction1(){
   //show the data
   //enable the click
   processing = false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

When you say button, do you mean the image being clicked? Or do you have a separate button also?
1

You can do this by taking advantage of the fact that animate takes a function to call upon completion of the animation. For example:

http://jsbin.com/adokiz/2

What we do is this:

$(document).ready(function() {
    var running = false;
    $('img').on('click', function(event) {
      if (running) return;

      running = true;
      console.log(this);
      $(this).animate({top: '400px'}, function() {
        $(this).css({top: 0});
        running = false;
      });
    });
});

With the global variable named running you can prevent multiple clicks from being recognized at the same time. This isn't exactly the same as your code but you can easily adapt it.

Another way to do this which should be even more resistant to being executed twice is using the jQuery one to bind it like so:

$(document).ready(function() {  
  var animation;

  var binding = function() {
    $('img').one('click', animation);
  };

  animation = function(event) {
    $(event.target).animate({top: '400px'}, function() {
      $(event.target).css({top: 0});
      binding();
    });
  };

  binding();
});

Demo: http://jsbin.com/adokiz/3

4 Comments

I tried it.But it didnt work on the way I expected.If I am clicking slowly it works fine.But if I am doing the samething continuously it didnt work .
You mean clicking continuously or what? Kind of lacking details on the "didn't work" part :).
Sorry I did a mistake from my side ..anyway same Idea above so I am accepting that answer due to posted earlier... :) Keep helping...
No problem. Added another way to do this that may or may not be better depending on how one works.
0

Use the unbind method:

var clickHandle = function(){
    $(this).unbind('click', clickHandle);
    callfunc();
};
var callfunc = function(){
    // do your stuff
    $('#imageid').click(clickHandle);// reinstall click handle
};
$('#imageid').click(clickHandle);

Comments

Your Answer

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.