0

I'm looking to change the color of font based on my timer reaching <= 30 seconds. I am using this countdown plugin as my timer. So far, I am able to change the color, etc... but it adds it regardless of the time, etc. I'm fairly new to jQuery, so any help is appreciated!

$(document).ready(function(){

    var seconds = $(".time-increment").val();

    $("#timer").countdown({
        until: +seconds, 
        format: 'MS', 
        layout: '{mn} {ml}, <span class="seconds-actual">{sn}</span> {sl} remaining',
        onExpiry: reload,
        onTick: checkFont
        }); 

    function reload() {
        var id = $("#item-content").attr('class');
        $.ajax({
            type: 'POST',
            url: '../modal_content.adam.php',
            data: 'Qwicksale_id='+id,
            success: function(msg) {
                $("#item-content").hide().html(msg).fadeIn('slow');
            }
        });
    }

});

UPDATED

I added a function for the onTick - The styled class is red all of the time, not just when seconds is less than or equal to 30?

function checkFont(seconds2) {
        if(seconds2[5] == 0 && seconds2[6] <= '30') {
            $('.hasCountdown').css('color', 'red');
        }
    }
6
  • Can you show your HTML? Specifically where class="seconds-actual". Commented Aug 5, 2013 at 17:58
  • you may simply use until: seconds no need of + there... Commented Aug 5, 2013 at 18:03
  • ".seconds-actual" is added by the countdown plugin. {sn} tag is the number that it is displaying - I seem to be able to get the value, but not able to test it correctly for my condition? Commented Aug 5, 2013 at 18:30
  • Solution: Using onTick callback, and testing for both the MINUTES and SECONDS column (since I only wanted the last 30 seconds of the countdown, not every last 30 seconds of every minute). Commented Aug 5, 2013 at 19:21
  • @Alpinestar22 What happens if you do seconds2[6] <= 30 (30 as a number, not a string)? Commented Aug 5, 2013 at 19:39

2 Answers 2

2

I believe the problem is that the if(seconds2 <= 30) { ... } block only executes once, when the page loads.

You need to use the onTick callback and check the remaining time in that handler.

See the second example on the "Callbacks" tab in the documentation: http://keith-wood.name/countdown.html

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

3 Comments

I believe this is the correct way to do it. It looks like it is an issue of reading the value of .seconds-actual... my condition seems to be what is not working?
If you look at the documentation, you can see that the remaining time is passed to the onTick handler. I would use that to check the time remaining instead of trying to read it from the DOM.
Great advice, I am now testing what the handler is passing back.. I still seem to have a hard time reading the value though. I seem to be getting the whole class red regardless of time left?
0

Its add the color regardless of the time because you are just checking if(seconds2 <= 30) on page load then changing the color if this condition is true regardless of the time...

You may want to check this inside the success function like so:

function reload() {
    var id = $("#item-content").attr('class');
    $.ajax({
        type: 'POST',
        url: '../modal_content.adam.php',
        data: 'Qwicksale_id='+id,
        success: function(msg) {
            $("#item-content").hide().html(msg).fadeIn('slow');
            if(seconds2 <= 30) {
               $('.hasCountdown').css('color', 'red');
            }
        }
    });
}`

You may want to try

if(seconds2 <= 30) {
   setTimeout(function() {
       $('.hasCountdown').css('color', 'red');
   }, seconds2);
}

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.