Javascript内存

Javascript内存,javascript,html,Javascript,Html,我想知道为什么我的程序在第一次匹配后就崩溃了……任何想法都将不胜感激。下面是代码片段。谢谢你的意见 var clicks = 0; //counts how may picks have been made in each turn var firstchoice; //stores index of first card selected var secondchoice; //stores index of second card selected var match = 0; //coun

我想知道为什么我的程序在第一次匹配后就崩溃了……任何想法都将不胜感激。下面是代码片段。谢谢你的意见

var clicks = 0; //counts how may picks have been made in each turn
var firstchoice; //stores index of first card selected
var secondchoice; //stores index of second card selected
var match = 0; //counts matches made
var backcard = "deck.jpg"; //shows back of card when turned over

var faces = []; //array to store card images
faces[0] = 'pic1.jpg';
faces[1] = 'pic2.jpg';
faces[2] = 'pic3.jpg';
faces[3] = 'pic3.jpg';
faces[4] = 'pic2.jpg';
faces[5] = 'pic1.jpg';

function choose(card) {
        if (clicks === 2) {
            return;
        }
        if (clicks === 0) {
            firstchoice = card;
            document.images[card].src = faces[card];
            clicks = 1;
        } else {
            clicks = 2;
            secondchoice = card;
            document.images[card].src = faces[card];
            timer = setInterval("check()", 1000);
        }
    }
    /* Check to see if a match is made */

function check() {
    clearInterval(timer); //stop timer
    if (faces[secondchoice] === faces[firstchoice]) {
        match++;
        document.getElementById("matches").innerHTML = match;
    } else {
        document.images[firstchoice].src = backcard;
        document.images[secondchoice].src = backcard;
        clicks = 0;
        return;
    }
}

setInterval的第一个参数必须是一个函数,而不是一个冒充函数的字符串。所以你想要这个:

function check() {
    clearInterval(timer); //stop timer
    if (faces[secondchoice] === faces[firstchoice]) {
        match++;
        document.getElementById("matches").innerHTML = match;
    } else {
        document.images[firstchoice].src = backcard;
        document.images[secondchoice].src = backcard;
    }
    clicks = 0;
}
timer=setInterval(函数(){check();},1000)

当然,您可以简化:

timer=setInterval(检查,1000)

不确定为什么在这里使用setInterval()。您可以更轻松地执行以下操作:

timer=setTimeout(检查,1000)

优点是在
check()
函数中没有要清除的间隔

另一个问题是,当存在匹配项时,您没有将“单击”计数器重置为0

你想要这个:

function check() {
    clearInterval(timer); //stop timer
    if (faces[secondchoice] === faces[firstchoice]) {
        match++;
        document.getElementById("matches").innerHTML = match;
    } else {
        document.images[firstchoice].src = backcard;
        document.images[secondchoice].src = backcard;
    }
    clicks = 0;
}

我认为你必须声明你的定时器函数是全局的。它仅在第一个函数的范围内定义,因此在第二个函数中,当您尝试清除它时,不会发生任何事情:

var timer = ''; //Declare timer up here first!

function choose(card) { ... }

function check() { ... }

你能解释一下这段代码应该做什么吗?firstchoice和secondchoice没有默认值。尝试使用默认值,并让我知道这是一个javascript代码,用于检查两个单击的图像是否匹配。如果他们匹配,牌将保持正面朝上,如果他们不匹配,他们将返回正面朝下的位置。。。。我还尝试将两个默认值都设置为0,并且程序在第一个matchvar定时器=''之后仍然没有响应;var firstchoice=0//存储所选第一张卡的索引var secondchoice=0//存储所选第二张卡的索引我现在添加了默认值和声明的计时器变量,但仍然是相同的问题。谢谢大家的评论,谢谢大家的帮助,感谢Rasmeister调试这个问题!