0

My Simple problem is Just to show that the time is running out.. For example There are only 10 seconds in the time, Show it in my Webpage that the time is 10.. 9.. 8.. 7.. 6.. 5.. 4.. 3.. 2.. 1.. and thats it.. its very simple.. i dont have an idea in javascript because im more in php codes.. I've tried this script

setInterval(function() {
var timer = $('span').html();
timer = timer.split(':');
var minutes = timer[0];
var seconds = timer[1];
seconds -= 1;
if (minutes < 0) return;
if (seconds < 0 && minutes != 0) {
    minutes -= 1;
    seconds = 59;
}
else if (seconds < 10 && length.seconds != 2) seconds = '0' + seconds;
if ((minutes < 10) && ((minutes+'').length < 2)) minutes = '0' + minutes;
$('span').html(minutes + ':' + seconds);
}, 1000);

and My html Tag like this

<font face="tahoma" size="2" color="red">
<span>00:10</span>
</font>

But its not working, but in JS fiddle.. it's working i wonder why when i put it in my webpage the 10 seconds is not moving.. Thank you for the help

7
  • 1
    Based on this but in JS fiddle.. it's working did you try enclosing the JS within <script></script> tag? Commented Nov 13, 2013 at 14:46
  • You should maybe also use $('span').text() as opposed to .html() Commented Nov 13, 2013 at 14:47
  • you worte using javascript in your title, but you also using jQuery for this, did you implement the jQuery library? if so, maybe a : $(document).ready(function(){}); is missing? Commented Nov 13, 2013 at 14:50
  • Are you sure that on your page just one span-node exist? Orherwise you should give your span a class or ID. Commented Nov 13, 2013 at 14:56
  • ...just as a side-note: length.seconds makes no sense in this case! Commented Nov 13, 2013 at 15:00

1 Answer 1

1

are you sure you are importing jquery    correcting the error NAN

 setInterval(function() {
var timer = $('span').html();
timer = timer.split(':');
var minutes = timer[0];
var seconds = timer[1];
    if(seconds!="00" || minutes!="00"){
        seconds -= 1;
        if (minutes < 0) return;
        if (seconds < 0 && minutes != 0) {
            minutes -= 1;
            seconds = 59;
        }
        else if (seconds < 10 && length.seconds != 2) seconds = '0' + seconds;
        if ((minutes < 10) && ((minutes+'').length < 2)) minutes = '0' + minutes;
        $('span').html(minutes + ':' + seconds);
    }
}, 1000);

Example : http://jsfiddle.net/Evmyp/4/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.