Jason
Jason

Reputation: 27

javascript disable second onclick

I have a javascript:

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">

    var Delay = 10;//Seconds after them clicking the link, the gateway vanishes. 
    function setupgateway()
    {

        var Left = $(window).width() /2;
                Left = Left - $('#gatewaydiv').width()/2;

        var Top = $(window).height() /2;
        Top = Top - $('#gatewaydiv').height()/2;

        $('#gatewaydiv').css('top', Top+'px').css('left', Left+'px').css('display', 'inline');
        $('#gatewayDimmer').width($('html').width());
        $('#gatewayDimmer').height($('html').height());
        $('#gatewayDimmer').css({display:'block', position:'fixed'});
    }

    function removegateway()
    {
        $('#gatewaydiv').css('display', 'none');
        $('#gatewayDimmer').css('display','none');
    }

    $(document).ready(function()
    {
                $('.my-link').click(function(e){
                e.preventDefault();

        $('.offerlink').click(function()
        {
            setTimeout('removegateway()', Delay*10);
        });

        setupgateway();
                });
    });
</script

...a pop-up, if you will, that's called/triggered by clicking an image(s) and goes away after x amount of seconds a link in the pop-up is clicked. I need the image(s)(callers/triggers) to go where it's(they're) linked to once clicked again, not call the javascript. What do I have to do to disable the javascript after the first click?

Upvotes: 1

Views: 3628

Answers (2)

Guffa
Guffa

Reputation: 700910

Put the code for binding the click in a function so that you can reuse it. Unbind the click when you show the gateway, and rebind it when you remove the gateway.

Note: The time unit for setTimeout is milliseconds, so Delay*10 would give you 0.1 seconds, not 10 seconds.

function bindLink() {
  $('.my-link').click(function(e){
    e.preventDefault();

    $('.offerlink').click(function() {
      window.setTimeout(function() {
        removegateway();
        bindLink();
      }, Delay * 1000);
    });

    setupgateway();
    $('.my-link').unbind('click');
  });
}


$(document).ready(function() {
  bindLink();
});

Upvotes: 0

Blender
Blender

Reputation: 298582

Use .one() instead of .click():

$('.my-link').one('click', function(e){
    e.preventDefault();

    $('.offerlink').click(function()
    {
        setTimeout(removegateway, Delay*10);
    });

    setupgateway();
});

.one() is like .click(), but is run only once and is then unbound.

Upvotes: 3

Related Questions