2

I used the following code to disable the onclick event . present code

function moveToDeckTable(game_table_id,category_id,status){
    var_game_table_id=game_table_id;
    var redirect="table.php";
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "ahr/addPlayer.php",
        data: {game_table_id:game_table_id,status:status},
        cache: false,
        success: function(data){
            if(data.success==1){
                PlayingStatus =setInterval(setPlayingStatus,1000);
                open_game_window = window.open(redirect+"?id="+data.game_table_id,"","_blank");
            }    
            else if(data.success==0)
                alert("User already playing..");
            else if(data.success==2)
                alert("You have not enough coins to play in this table .buy more coins or play in minor coins table.");    
        }
    }); 
    //code to disable onclick

}
function setPlayingStatus(){
    if(open_game_window.closed){
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "ahr/setPlayingStatus.php",
            cache: false,
            success: function(data)
            {
                clearInterval(PlayingStatus);
            }
        }); 
        //code to re enable onclick
    }    
}

Now , I want to enable that event again on another button click. How can I enable the onclick event?

6
  • How did that event attach to that element at start? It use onclick or $(ele).on('click')? Commented Nov 21, 2015 at 6:06
  • is there another way to disable enable the onclick event? Commented Nov 21, 2015 at 6:07
  • Why disable it? I'm writing an answer right now ;) Commented Nov 21, 2015 at 6:10
  • @ fuyushimoya onclick Commented Nov 21, 2015 at 6:13
  • Ok, just one question. How many of these clickable elements do you have? How many buttons are you wanting to link to these function/click events? Commented Nov 21, 2015 at 6:43

3 Answers 3

2

Check this out. It doesn't need you to cancel or "reinitialize" events at all.

window.onload = function() {
  //capture the click event
  var eventsEnabled = true;

  var log = document.getElementById('log');

  document.getElementById('button').onclick = function() {
    //if capturing events is true
    if (eventsEnabled == true) {
      log.innerHTML = 'event captured';
    }
  };



  document.getElementById('captureStop').onclick = function() {
    eventsEnabled = false;
    log.innerHTML = 'capturing turned off';
  };

  document.getElementById('captureStart').onclick = function() {
    eventsEnabled = true;
    log.innerHTML = 'capturing turned on';
  };
};
<input type="button" id="button" value="This is a button">
<input type="button" id="captureStop" value="Stop the evil clicking (capturing)">
<input type="button" id="captureStart" value="Start the evil clicking (capturing)">
<div id="log"></div>

UPDATE: There is really no way for me to test this code, so you will have to tell me if it works or not. You/I will improve it from there and conclude this issue.

This is some of your existing code. We need to toggle disable the functions. It appears simple enough. Just adapt the code in my "old" answer to function for this purpose.

function moveToDeckTable(game_table_id,category_id,status){
    if(captureEvents == true){//your if (see my first answer [above])
    var_game_table_id=game_table_id;
    var redirect="table.php";
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "ahr/addPlayer.php",
        data: {game_table_id:game_table_id,status:status},
        cache: false,
        success: function(data){
            if(data.success==1){
                PlayingStatus =setInterval(setPlayingStatus,1000);
                open_game_window = window.open(redirect+"?id="+data.game_table_id,"","_blank");
            }    
            else if(data.success==0)
                alert("User already playing..");
            else if(data.success==2)
                alert("You have not enough coins to play in this table .buy more coins or play in minor coins table.");    
        }
    }); 
    //code to disable onclik
  }
}
function setPlayingStatus(){
    if(captureEvents == false){
    if(open_game_window.closed){
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "ahr/setPlayingStatus.php",
            cache: false,
            success: function(data)
            {
                clearInterval(PlayingStatus);
            }
        }); 
        //code to re enable onclik
    }  
   }   
}
Sign up to request clarification or add additional context in comments.

10 Comments

in my code i used "game_table_id" in selector that is passed by function parameter so i need to use onclick function insted .onclick.
Could you better explain why this code isn't going to work for you? I'm sorry, I don't understand the different between onclick and .onclick.
I will revise my answer if it doesn't work for you ;)
Ok, just one question. How many of these clickable elements do you have? How many buttons are you wanting to link to these function/click events?
i have dynamic button list like button18 ,button19 etc not final. i just want like on button click open new window and if one window opend i want to disable the onclick so user cant be able to open more than one window .and when window closed i will re enable the onclick so user able to re open the window.
|
0
function enable(){
$( "body" ).delegate( '#btn'+game_table_id+' > .green_btn', "click", function() {
// your code
});
}

onClick of other function call enable function it add event for the button

Comments

0

Working code. @www139 thanks the updated code. function1-disable the onclick if captureEvents true

function2-enable the onclick if captureEvents false

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.