【发布时间】:2015-02-20 17:26:57
【问题描述】:
我对 Javascript 很陌生,似乎我没有正确理解 if else 语句。
我有一个脚本可以让访问者访问 4 个网站中的 1 个,但我的代码中的最后 2 个网站不起作用。
<script>
setTimeout(function() {
var r = Math.random();
if(r > 0.49) {
window.location.replace("1.html");
}
else if(r < 0.48) {
window.location.replace("2.html");
}
if (r == 0.48){
window.location.replace("maybe.html");
}
else if (r == 0.49){
window.location.replace("4.html");
}
}, 1);
</script>
我的代码现在的样子。它需要如何工作才能发挥作用?
【问题讨论】:
-
设置 1 毫秒的超时有什么意义?
-
请定义“不起作用”。
-
代码似乎没问题,但是 math.random 返回 0.48 和 0.49 的可能性有多大?这是非常罕见的。
-
@blex 谢谢,不知道。
标签: javascript html