2013-04-15 36 views
0
<script> 
function oneortwo(){ 
var num = Math.floor((Math.random()*2)+1); 
alert(num); 
} 
</script> 
<button onclick="oneortwo()">click</button> 

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

+0

设置每次点击按钮的值,那么你可以告诉他们最后点击,哪些将其更改为。 –

回答

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

谢谢!天才 :) – stackoverflower

0

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

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); 
} 
1

喜欢这个?

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