2014-08-30 60 views
0

我想在smartry(.tpl)文件中使用此計數器。我已經找到腳本鏈接低於沒有新鮮的JavaScript增量計數器

DEMO

我只是想知道我是否需要把這些腳本代碼的

var START_DATE = new Date("October 10, 2012 22:30:00"); // put in the starting date here 
var INTERVAL = 1; // in seconds 
var INCREMENT = 30; // increase per tick 
var START_VALUE = 35001; // initial value when it's the start date 
var count = 0; 

$(document).ready(function() { 
var msInterval = INTERVAL * 1000; 
var now = new Date(); 
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE; 
document.getElementById('counter').innerHTML = count; 

window.setInterval(function(){ 
    count += INCREMENT; 
    document.getElementById('counter').innerHTML = count; 
}, msInterval); 

}); 

我的意思是在哪裏把這些下還是?我想在smarty(.tpl)文件中使用它們。指導我,因爲我是新的JavaScript。

回答

0

根據您的需要,您可以將此代碼放入您的Smarty模板或JS文件中。

當然你也需要加載jQuery。

下面有一個使用您的代碼和工作沒有問題全Smarty的.tpl文件:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Demo counter</title> 
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> 
</head> 
<body> 


<div id="counter"> 


</div> 

<script type="text/javascript"> 
    {literal} 
    var START_DATE = new Date("October 10, 2012 22:30:00"); // put in the starting date here 
    var INTERVAL = 1; // in seconds 
    var INCREMENT = 30; // increase per tick 
    var START_VALUE = 35001; // initial value when it's the start date 
    var count = 0; 

    $(document).ready(function() { 
     var msInterval = INTERVAL * 1000; 
     var now = new Date(); 
     count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE; 
     document.getElementById('counter').innerHTML = count; 

     window.setInterval(function() { 
      count += INCREMENT; 
      document.getElementById('counter').innerHTML = count; 
     }, msInterval); 

    }); 
    {/literal} 
</script> 

</body> 
</html>