2013-10-31 106 views
0

這是我廢棄togheter腳本,它只是一個基本的停止時間觀察腳本,我想要一些輸入如何添加簡歷和開始按鈕。而且我希望能夠在它停止時暫停時發送ajax調用。開始停止恢復阿賈克斯

var timer; 
$(function() { 
    $('#dateTime').html(getDateTime()); 
    timer = setTimeout(function() { 
     update(); 
    }, 1000); 
    $('#btn').click(function() { 
     clearTimeout(timer); 
     var currentdatetime = getDateTime(); 
    }); 
}); 

function update() { 
    $('#dateTime').html(getDateTime()); 

    timer = setTimeout(function() { 
     update(); 
    }, 1000); 
} 

function getDateTime() { 
    var currentdate = new Date(); 
    var datetime = "Timer: " + currentdate.getDate() + "/" + (currentdate.getMonth() + 1) + "/" + currentdate.getFullYear() + " " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds(); 
    return datetime; 
} 

回答

0
var timer; 

function resume() { 
    update(); 
    $('#resume').off("click", resume); 
    $('#pause').on("click", pause); 
    // make start/resume ajax call here 
} 

function pause() { 
    clearTimeout(timer); 
    $('#resume').on("click", resume); 
    $('#pause').off("click", pause); 
    // make pause/stop ajax call here 
} 

function update() { 
    $('#dateTime').html(getDateTime()); 

    timer = setTimeout(function() { 
     update(); 
    }, 1000); 
} 

function getDateTime() { 
    var currentdate = new Date(); 
    var datetime = "Timer: " + currentdate.getDate() + "/" + (currentdate.getMonth() + 1) + "/" + currentdate.getFullYear() + " " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds(); 
    return datetime; 
} 

$(function() { 
    resume(); 
});