2014-04-13 55 views
-1

非常瞭解JavaScript,並一直在尋找如何讓計數器計時器工作。 這裏是我的JSfiddle的鏈接。對於發生了什麼問題得到一些反饋會很棒。 謝謝。計數器計時器JavaScript

<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

......這樣有什麼不好? –

回答

1

應該if(secs == 59)而不是if(sec == 59),因爲你的變量命名爲secs

+0

天才哈哈。謝啦 – Nolemonpledge