<script> function oneortwo(){ var num = Math.floor((Math.random()*2)+1); alert(num); } </script> <button onclick="oneortwo()">click</button>
上面的脚本是随机化一和二。我想提醒一个,然后是两个,然后是一个,然后是两个......等等。如果一个被警告,下一个值将是两个,反之亦然。基本上,如果已经发出警报,我不希望重复以前的值。任何帮助将不胜感激。谢谢 :)
var num = Math.floor((Math.random() * 2) + 1);
function oneortwo(){
num = num == 1 ? 2 : 1;
alert(num);
}
像这样?
var counter = Math.floor((Math.random()*2)+1); // initially random
function oneortwo(){
counter = (counter == 2) ? 1 : 2;
alert(num);
}
为了保持最初的“随机性”,这应该可行。
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);
}