jquery interval impulse指定一个新的随机而不是检查正确的答案

时间:2013-09-16 17:07:35

标签: c# javascript jquery asp.net

我在下面用aspx制作了这个jQuery游戏,它绑定到间隔3000.

$(document).ready(function () {
    var interval;                                    //start timer
    interval = setInterval(function () {             //write timer function
        var num1 = parseInt($("#Label1").html());    //number1 is my label1
        var num2 = parseInt($("#Label3").html());    //number2 is my label3
        var total = num1 + num2;                     //find total of these two
        var entry = parseInt($("#TextBox1").val());  //the number your enter is entry
        if (entry == total)                          //the timer will check every 100 ms if they match
            $(".car").css("left", "+=25px");         //if they match, move the car 25 px to the right
            $('#Button1').trigger('click');          //and trigger button1, so button1 throws new dice and car doesn't move further
    }, 3000);
});

我希望间隔脉冲检查数字是否匹配并沿x轴移动.car div。但是,我得到冲动每3000毫秒指定一个新的随机变量,而不是检查条目和总变量是否相等,然后向右行驶+ + 25 px。为什么会这样?

1 个答案:

答案 0 :(得分:1)

我认为你的意思是按钮触发器实际上是等于检查的一部分,所以你需要括号:

if (entry == total){              //the timer will check every 100 ms if they match
    $(".car").css("left", "+=25px");  //if they match, move the car 25 px to the right
    $('#Button1').trigger('click');   //and trigger button1, so button1 throws new dice and car doesn't move further
}