0

让我首先说我不是 javascript 方面的专家,但我已经完成了我的研究并且不能完全弄清楚我的代码做错了什么。对于我的网页,我一直在编辑一个与反射相关的游戏,它基本上有四个不同的警报,基于他们按下停止按钮的速度。

这是我的代码的一部分,它将显示我的重定向页面,我目前没有被重定向到。无论您收到哪个警报,都会将您带到最后一个,这让我想知道我哪里出错了。

function remark(responseTime) {
    var responseString = "";
    if (responseTime < 0.20) responseString = "well done!.";
    window.location.href = "dfgr454.php";
    if (responseTime >= 0.20 && responseTime < 0.40) responseString = "nice.";
    window.location.href = "fdkjgtry5.php";
    if (responseTime >= 0.40 && responseTime < 0.60) responseString = "could be better. ";
    window.location.href = "dfg5654f.php";
    if (responseTime >= 0.60 && responseTime < 0.80) responseString = "that's no good.";
    window.location.href = "bvcb56.php";
    if (responseTime >= 0.80 && responseTime < 1) responseString = "have you been drinking?";
    window.location.href = "dfgf643re.php";
    if (responseTime >= 1) responseString = "did you fall asleep?";
    return responseString;
}

如果它有帮助(我不完全确定它会,因为它对我没有帮助),当我编辑我的代码以尝试 window.open 时,所有这些都会立即打开。有没有办法阻止这种情况发生?警报很好,但我希望它们重定向到我试图与它们配对的特定 window.location。请并感谢您,任何建议都是完美的!

4

1 回答 1

1

正确的语法是

if(condition1) {
    /* your code */
} else if(condition2) {
    /* other code */
}

所以你的代码应该是这样的:

function remark(responseTime)
{
    var responseString="";
    if (responseTime < 0.20) {
        responseString="well done!.";
        window.location.href="dfgr454.php";
    } else if (responseTime >= 0.20 && responseTime < 0.40) {
        responseString="nice.";
        window.location.href="fdkjgtry5.php";
    } else if (responseTime >=0.40 && responseTime < 0.60) {
        responseString="could be better. ";
        window.location.href="dfg5654f.php";
    } else if (responseTime >=0.60 && responseTime < 0.80) {
        responseString="that's no good.";
        window.location.href= "bvcb56.php";
    } else if (responseTime >=0.80 && responseTime < 1) {
        responseString="have you been drinking?";
        window.location.href="dfgf643re.php";
    } else if (responseTime >=1) {
        responseString="did you fall asleep?";
    }

    return responseString;
}

如果您if在另一个之后使用一个语句而不是else if,则它们将相互独立地进行测试。因此,如果您的响应时间是< 0.2它也会< 1导致您意想不到的结果。

于 2013-10-30T07:39:35.860 回答