1

你好我有一些问题,如果点击#health,如何阻止点击功能

运行单击功能时,我可以再次单击,但如何单击块 2。

$('#health').click(function() {
  var count = <?=$wait_time?>;
  countdown = setInterval(function(){
    $("p#health").html(count + " seconds remaining!");
    if (count == 0) {
      $.ajax({
       url: 'pages/map/hospital.php?heal=1',
       success: function(data) {
        $("p#health").hide();
       }
      });
    }
    count--;
  }, 1000);
});
4

3 回答 3

2

使用.one()方法而不是.click()

$('#health').one('click', function() {
    //etc
});

要清除间隔,只需使用以下clearInterval()方法:

clearInterval(countdown);
于 2012-09-16T01:12:43.350 回答
0
var state = true;
$(document).click(function() {
    if (state === true) {
        var count = < ? = $wait_time ? > ;
        countdown = setInterval(function() {
            $("p#health").html(count + " seconds remaining!");
            if (count == 0) {
                $.ajax({
                    url: 'pages/map/hospital.php?heal=1',
                    success: function(data) {
                        $("p#health").hide();
                    }
                });
            }
            count--;
        }, 1000);

        state = false;
    }
});
于 2012-09-16T01:13:27.387 回答
0

如果您想阻止后续点击执行任何操作,请尝试以下操作:

$('#health').click(function() {
  if(countdown)
    return false;

  var count = <?=$wait_time?>;
  countdown = setInterval(function(){
    $("p#health").html(count + " seconds remaining!");
    if (count == 0) {
      $.ajax({
       url: 'pages/map/hospital.php?heal=1',
       success: function(data) {
        $("p#health").hide();
       }
      });
    }
    count--;
  }, 1000);
});
于 2012-09-16T01:13:31.430 回答