2012-03-23 78 views
-2

不確定下面的代碼是100%正確的,但它正在做這項工作,現在需要轉換爲jQuery。JavaScript到jQuery轉換需要

var count = 0.00; 
var currency = "R$"; 

function doCount() { 
    count = count + 1.99; 
    document.getElementById("number").innerHTML = currency + parseFloat(count).toFixed(2).replace(/\./g, ','); 
    var tim = setTimeout('doCount()', 60000); // increment every 60 seconds 
} 
doCount(); 
+3

如果你想讓它每60秒發生,使用'的setInterval()'一次,而不是在創建每個函數調用一個新的超時。你還應該傳遞函數引用('setInterval(doCount,60000)')而不是字符串('setInterval('doCount()',60000)')。 – 2012-03-23 10:42:07

+0

Firebug彈出doCount未定義!任何提示? – memo 2012-03-23 11:07:12

+0

確保你在'doCount()'的函數聲明後調用'setInterval(doCount,60000)'**。 – 2012-03-23 11:28:21

回答

3
var count = 0.00; 
var currency = "R$"; 

function doCount() { 
    count = count + 1.99; 
    $("#number").html(
    currency + parseFloat(count).toFixed(2).replace(/./g, ',') 
); 
    var tim = setTimeout('doCount()', 60000); // increment every 60 seconds 
} 
doCount(); 
5

jQuery是不是一種語言,它只是一個庫,讓您簡單的JavaScript編寫。如果它是這樣工作的,則不需要將其轉換爲jQuery。

0

使用setInterval

var count = 0.00, 
    currency = "R$"; 

setInterval(function { 
    count = count + 1.99; 
    $("#number").html(
     currency + parseFloat(count).toFixed(2).replace(/./g, ',') 
    ); 
}, 60*1000); // increment every 60 seconds 

感謝@deadrunk