Count Up Timer JavaScript

时间:2014-04-13 15:57:13

标签: javascript timer

非常了解javascript并一直在查找如何让计时器工作。 这是我的JSfiddle的链接。能够得到一些关于错误的反馈会很棒。 感谢。

<script type="text/javascript">
$(function () {
var arr = 
["https://botanicalappbeta.s3.amazonaws.com/uploads/plant_photo/image/1512/paphioacmodontum.jpg", "https://botanicalappbeta.s3.amazonaws.com/uploads/plant_photo/image/1250/aechmea1.jpg"];

$("#puzzle div").css({'background-image':'url(' +arr[Math.floor(Math.random()*arr.length)] + ')'});

var puzzle = $("#puzzle");
var pieces = $("#puzzle div");

pieces.sort(function (a, b) {
    var temp = parseInt(Math.random() * 100);
    var isOddOrEven = temp % 2;
    var isPosOrNeg = temp > 5 ? 1 : -1;
    return (isOddOrEven * isPosOrNeg);
}).appendTo(puzzle);

var timer;
var secs = 0;
var mins = 0;
var timeField = document.getElementById("time");
timeField.innerHTML = "00:00";

function update(){
if(sec == 59){
    mins++;
    secs = 0;
} 
else {
    secs++;
}
if(secs<10){
    timeField.innerHTML = mins + ':0' + secs;
} 
else {
    timeField.innerHTML = mins + ':' + secs;
}   
}

function start(){
timer = setInterval(function() {update()}, 1000);
}

start();    
initSwap();

function initSwap() {
initDroppable($("#puzzle div"));
initDraggable($("#puzzle div"));
}


function initDraggable($elements) {
$elements.draggable({
    appendTo: "body",
    helper: "clone",
    cursor: "move",
    revert: "invalid"
});
}

function initDroppable($elements) {
$elements.droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-drop-hover",
    accept: ":not(.ui-sortable-helper)",
    over: function (event, ui) {
        var $this = $(this);
    },
    drop: function (event, ui) {
        var $this = $(this);
        var linew1 = $(this).after(ui.draggable.clone());
        var linew2 = $(ui.draggable).after($(this).clone());
        $(ui.draggable).remove();
        $(this).remove();
        initSwap();
    }
});
}
});
</script>

这是JSFiddle!

http://jsfiddle.net/QY5jb/1/

1 个答案:

答案 0 :(得分:1)

应该是if(secs == 59)而不是if(sec == 59),因为您的变量名为secs