2013-09-30 21 views
0

我很小。这有点不好意思。我设法通过我的span标签中的javascript/jquery获得当前时间。首先要实现我的真实意图。jquery从当前时间每秒钟添加“随机”的东西值?

我想要一个脚本,在10秒(从我当前获得的时间)到当前的跨班级.value类的时间中,每秒增加一定数量(0到5之间)。有点混乱,但我知道我会尽力来形容它以这种方式:

pressume,目前的时间是30/9/2013 - 10:38:47

和十秒钟,功能会从0至5

添加随机值
1st second - time: 30/9/2013 - 10:38:47 - adds: +3 - displays: 103 
2nd second - time: 30/9/2013 - 10:38:48 - adds: +1 - displays: 104 
3th second - time: 30/9/2013 - 10:38:49 - adds: +5 - displays: 109 
4th second - time: 30/9/2013 - 10:38:50 - adds: +0 - displays: 109 
5th second - time: 30/9/2013 - 10:38:51 - adds: +2 - displays: 111 
6th second - time: 30/9/2013 - 10:38:52 - adds: +4 - displays: 115 
...(up to)... 
10th second - time: 30/9/2013 - 10:38:56 - adds: +2 - displays: 133 

and now from beggining.. 

我不知道怎么做这样的事情,所以我需要一些帮助,开始点什么

目前我的项目只有两个跨度和显示当前时间:http://jsfiddle.net/a76wa/

HTML:

<span class="value">100</span> 
</br> 
<span class="new-time"></span> 

脚本:

var currentdate = new Date(); 
var datetime = currentdate.getDate() + "/" + (currentdate.getMonth()+1) + "/" + currentdate.getFullYear() + " - " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds(); 

$(".new-time").text(datetime); 

任何帮助或建议表示赞赏。

+0

我不明白你要达到什么目的。如果添加3,“30/9/2013 - 10:38:47”如何变成“103”?你将什么加入“3”? –

+0

不,时间不会变成103,时间是起点.. – dzordz

回答

0

我认为这是你在找什么:http://jsfiddle.net/jonigiuro/Q6wpu/

var startVal = $('.value'); 
var newTime = $('.new-time'); 
var startLink = $('.start'); 

startLink.on('click', function() { 
    var tracker = 0; //varialbe to count the passed seconds 
    var startValue = parseInt(startVal.text()); //get the start value 
    var t = setInterval(function() { 
     tracker++; //add 1 each second 
     var randomNumber = Math.round(Math.random() * 10); //generate a random integer between 0 and 10 
     startValue += randomNumber; //add the start value and the random number 
     newTime.text(startValue); //change the text 
     if(tracker === 10) { //after ten seconds 
      clearInterval(t); //stop the timer 
      startVal.text(startValue); //and change the start value 
     } 
    }, 1000); 
}); 

你需要点击“启动计数器”才能启动它

0

使用SetInterval()功能做你的逻辑,只要把你的函数,它被添加随机秒到当前的时间每一秒:

setInterval(function() { 
     // Do something every second 
}, 1000); 
+0

是的,我目前正在试验一些与此有关的东西,但是谢谢,如果我得到一些有用的东西, – dzordz