我正在尝试调试我拥有的 .js 文件。该文件使用 if/then 返回变量。每个变量设置为 location.replace("url")
,因此在任何给定点都应该返回一个 url。
我尝试替换每个变量的 location.replace
与 console.log
,希望它只是返回一个函数预期的结果。相反,它立即返回每个变量并只记录它们的底层 URL。
下面是一段简短的 JSfiddle:https://jsfiddle.net/j_Sani/ehutcnmb/12/
这是一个更长的 JSfiddle,包含更彻底的变量: https://jsfiddle.net/j_Sani/ehutcnmb/33/
<head>
<script>
var t1 = location.replace("https://stackoverflow.com/");
var t2 = location.replace("https://www.google.com/");
var t3 = location.replace("https://www.bing.com/");
const now = new Date();
const s1 = now.getHours() === 15;
const s2 = now.getHours() === 16;
function go2() {
if (s1) {
return t1;
} else if (s2) {
return t2;
} else {
return t3;
}
}
</script>
</head>
基本上现在,当我在浏览器中加载文档时,它会重新路由到最终的 else
通过返回 t3
。如果我在声明中得到任何其他变量,我希望它。在我的 JSfiddle 中,我包含了一个更长的版本,其中包含更多 else if
的。
欢迎所有意见。
最佳答案
这样写,location
函数就会立即执行。
var t3 = location.replace("https://www.bing.com/");
您需要将该语句放入 if
语句中。
关于javascript - if/else 语句仅返回最终条件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57762152/