jQuery:在我的代码中添加新功能会停止所有其他功能

时间:2018-11-03 13:15:20

标签: jquery

我在构建基于温度的计时器时遇到了麻烦。我有一个启动功能,并具有升高和降低所需温度的功能,然后以秒为单位进行倒数计时。代码:

$(document).ready(function(){
var desiredTemp = 10;
var actualTemp = 12;
var tempMatch = false;
var degreeTime = 5;
var seconds = degreeTime;


$("#actualBox").text(timer(seconds));
/*This returns the outcome of the tick and counter        functions.*/
function timer(seconds) {
    return actualTemp;
}
/*This checks whether the temp should drop by one or not.*/
function tick() {
    if(seconds === 0) {
        actualTemp -= 1;
        seconds = degreeTime;
    }
    else {
        seconds -= 1;
        $("#actualBox").text(timer(seconds));
    }
}
/*this sets the count interval to run the tick     function every second*/
function counter() {
    count = setInterval(tick, 1000);
}


/*functions to raise and lower desired temp*/
function changeTemp(degree) {
    desiredTemp += degree;
}

function showDesired() {
    return desiredTemp;
}


/*when you click Start, this runs the counter*/
$("#start").click(function(){
    counter();
});

$('#lowerTemp').click(function(){
    changeTemp(-1);
    $('#desiredBox').text(showDesired());
});

$('#raiseTemp').click(function(){
    changeTemp(1);
    $('#desiredBox').text(showDesired());
});

});

现在,当我在任何地方插入此代码时:

function doneCheck() {
    if actualTemp === desiredTemp;
    jQuery('#actualBox').css({"background-color:    green"});
}

计时器和更改温度功能完全停止工作。我已经搜索过,但没有找到有关原因的任何相关信息。有人可以指出正确的方向吗?

2 个答案:

答案 0 :(得分:1)

修复语法错误

function doneCheck() {
    if(actualTemp == desiredTemp)
       $('#actualBox').css({"background-color: green;"});
}

答案 1 :(得分:0)

有一些语法错误可以纠正这些错误,并在需要的地方调用doneCheck()函数。

var desiredTemp = 10;
var actualTemp = 12;
var tempMatch = false;
var degreeTime = 5;
var seconds = degreeTime;

$(document).ready(function(){


$("#actualBox").text(timer(seconds));
/*This returns the outcome of the tick and counter        functions.*/
function timer(seconds) {
    return actualTemp;
}
/*This checks whether the temp should drop by one or not.*/
function tick() {
    if(seconds === 0) {
        actualTemp -= 1;
        seconds = degreeTime;
    }
    else {
        seconds -= 1;
        $("#actualBox").text(timer(seconds));
    }
}
/*this sets the count interval to run the tick     function every second*/
function counter() {
    count = setInterval(tick, 1000);
}


/*functions to raise and lower desired temp*/
function changeTemp(degree) {
    desiredTemp += degree;
}

function showDesired() {
    return desiredTemp;
}


/*when you click Start, this runs the counter*/
$("#start").click(function(){
    counter();
});

$('#lowerTemp').click(function(){
    changeTemp(-1);
    $('#desiredBox').text(showDesired());
});

$('#raiseTemp').click(function(){
    changeTemp(1);
    $('#desiredBox').text(showDesired());
});

});


function doneCheck() {
  if(actualTemp === desiredTemp)
    jQuery('#actualBox').css("background-color","green");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="start">start</button>
<button id="lowerTemp">lowerTemp</button>
<button id="raiseTemp">raiseTemp</button>
<button id="desiredBox">desiredBox</button>
<button id="actualBox">actualBox</button>