jquery - Update individual span tags counting upwards from x seconds -


i have several <span> tags hold seconds.
example:
<span class="timeout">27</span>
<span class="timeout">58</span>

i trying increment them 1 second each time, having difficulties, namely, puts wrong data in <span> tags.

i must doing wrong, below code trying use..
must wrong , full of mistakes, well, i'm still trying learn ;-)

function timer() {     var self = $(this);     var idx  = $("span.timeout");     var sec = parseint(self.find('span.timeout').text())     var interval = setinterval(function() {         idx.each(function() {             idx.text(sec++)         });     }, 1000); } 

thanks in advance,
- robert.

you want way:

function timer() {     var self = $(this);     var idx = $("span.timeout");     //console.log(self.find('span.timeout').text()); //gets nothing     var interval = setinterval(function () {         idx.text(function (_, txt) { //use .text()             return parseint(txt, 10) + 1; //convert current text int , increment it.         });     }, 1000); } 

fiddle

your issue here var sec = parseint(self.find('span.timeout').text()) self nothing window in case , trying find spans in them , getting text of it. gets nothing , doing parseint on nothing gets nan , using calculation, wrong. can use callback of text function , current textvalue of span , return updated value span.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -