【问题标题】:jquery alert one then two then one then two... and so onjquery alert 一个然后两个然后一个然后两个...等等
【发布时间】:2013-04-15 20:49:04
【问题描述】:
<script>
function oneortwo(){
var num = Math.floor((Math.random()*2)+1);
alert(num);
}
</script>
<button onclick="oneortwo()">click</button>

上面的脚本是随机化一和二。 我想提醒一个,然后是两个,然后是一个,然后是两个......等等。 如果一个被警告,下一个值将是两个,反之亦然。 基本上,如果已经发出警报,我不希望重复先前的值。 任何帮助将不胜感激。谢谢:)

【问题讨论】:

  • 在每次点击时设置按钮的值,然后你就可以知道他们最后点击了什么以及将其更改为什么。

标签: jquery random alert


【解决方案1】:
var num = Math.floor((Math.random() * 2) + 1);
function oneortwo(){
    num = num == 1 ? 2 : 1;
    alert(num);
}

【讨论】:

    【解决方案2】:

    像这样?

    var counter = Math.floor((Math.random()*2)+1); // initially random
    function oneortwo(){
        counter = (counter == 2) ? 1 : 2; 
        alert(num);
    }
    

    【讨论】:

      【解决方案3】:

      为了保持最初的“随机性”,这应该可行。

      var num;
      function oneortwo() {
          if (num == 1) num = 2;
          else if (num == 2) num = 1;
          else num = Math.floor((Math.random() * 2) + 1);
          alert(num);
      }
      

      【讨论】:

        猜你喜欢
        • 2018-08-17
        • 1970-01-01
        • 2020-11-03
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-29
        相关资源
        最近更新 更多