0

我正在创建一个在线游戏,玩家必须在关闭弹出窗口时回答问题。如果他们回答错误,他们会收到警告,告诉他们他们是否不正确。如果他们回答正确,他们会收到下一个问题的弹出窗口。我的功能不正确,但如果正确的位工作,我无法弹出。我究竟做错了什么?请注意,我对 Javascript 很陌生,所以这几乎完全是其他人的代码。

            function Start1(URL1, WIDTH, HEIGHT) {
            windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT;
            preview = window.open(URL1, "", windowprops);
        }


        function doPopup1() {
            url1 = "focus.html";
            width = 500;  // width of window in pixels
            height = 500; // height of window in pixels
            delay = 5;    // time in seconds before popup opens
            timer = setInterval("Start1(url1, width, height)", delay*1000);
        } 

        function doPopup2() {
            url2 = "q1.html";
            width = 500;  // width of window in pixels
            height = 500; // height of window in pixels
            delay = 1;    // time in seconds before popup opens
            timer = setTimeout("Start1(url2, width, height)", delay*1000);
        }        


function checkAnswer (form){
        var Answer = form.Answer.value;
        if (Answer == "2"){
            doPopup2();
        }
        else{
            alert ("Incorrect")
        }
    }

<body OnLoad="doPopup1();">

然后是一个基本的 HTML 表格,但重要的部分是:

<input type="submit" name="Submit" value="Submit" onClick="checkAnswer(this.form)" />
4

1 回答 1

0

我根据您的最后评论简化了您的代码并尝试了这个:

function Start1(URL1, WIDTH, HEIGHT) {
    console.log('Start1 called:' + URL1);
}

function doPopup2() {
    url2 = "q1.html";
    width = 500; // width of window in pixels
    height = 500; // height of window in pixels
    delay = 1; // time in seconds before popup opens
    timer = setTimeout("Start1(url2, width, height)", delay * 1000);
}

doPopup2();

在我的开发工具中,这导致错误Start1 is not defined.被记录到控制台。一般来说,用字符串调用 setTimeout 是一个非常糟糕的主意。您将希望用函数替换该字符串。我们将使用一个匿名函数,以便我们可以传入适当的参数。

function Start1(URL1, WIDTH, HEIGHT) {
    console.log('Start1 called:' + URL1);
}

function doPopup2() {
    url2 = "q1.html";
    width = 500; // width of window in pixels
    height = 500; // height of window in pixels
    delay = 1; // time in seconds before popup opens
    timer = setTimeout(function() { Start1(url2, width, height); }, delay * 1000);
}

doPopup2();​

多一点清理,这应该对你有用。确保您声明变量,var否则它们将成为全局变量。我猜这就是为什么你在一些地方有 url1 和 url2 的原因。

function showPopup(url, width, height) {
    var windowprops = "left=50,top=50,width=" + width + ",height=" + height;
    return window.open(url, "", windowprops);
}

function doPopup1() {
    var url = "focus.html";
    var width = 500; // width of window in pixels
    var height = 500; // height of window in pixels
    var delay = 5; // time in seconds before popup opens
    return setTimeout(function() {
        showPopup(url, width, height);
    }, delay * 1000);
}

function doPopup2() {
    var url = "q1.html";
    var width = 500; // width of window in pixels
    var height = 500; // height of window in pixels
    var delay = 1; // time in seconds before popup opens
    return setTimeout(function() {
        showPopup(url, width, height);
    }, delay * 1000);
}


function checkAnswer(form) {
    var answer = form.Answer.value;
    if (answer == "2") {
        doPopup2();
    }
    else {
        alert("Incorrect")
    }
}​
于 2012-05-05T19:24:16.757 回答