Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6139065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:56:36+00:00 2026-05-23T17:56:36+00:00

Hi I am having a problem where scoping seems to be lost. What I

  • 0

Hi I am having a problem where scoping seems to be lost. What I am doing wrong?

Suppose the logic is that 1 second is decreased from every counter and not from the last counter only.
What I am doing wrong?

<html>
<head>
    <link rel="stylesheet" href="http://static.jquery.com/files/rocker/css/reset.css" type="text/css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
        (function($){
            $.fn.extend({

                countdown: function(options) {

                var defaults = {
                    daysSelector : 'span.days',
                    hoursSelector : 'span.hours',
                    minutesSelector : 'span.minutes',
                    secondsSelector : 'span.seconds'
                }

                var options =  $.extend(defaults, options);
                var _this  = $(this);

                tick =  function()
                {
                    var days = _this.find(options.daysSelector);
                    var hours = _this.find(options.hoursSelector);
                    var minutes = _this.find(options.minutesSelector);
                    var seconds = _this.find(options.secondsSelector);
                    console.log(_this);
                    var currentSeconds=seconds.text();
                    currentSeconds--;
                    if(currentSeconds<0)
                    {
                        seconds.text("59");
                        var currentMinutes=minutes.text();
                        currentMinutes--;
                        if(currentMinutes<0)
                        {
                            (minutes).text("59");
                            var currentHours=(hours).text();
                            currentHours--;
                            if(currentHours<0)
                            {
                                (hours).text("23"); 
                                var currentDays=(hours).text();
                                currentDays--;
                            }
                            else
                            {
                                if(currentHours.toString().length==1)
                                {
                                    (hours).text('0'+currentHours);
                                }
                                else
                                {
                                    (hours).text(currentHours);
                                }
                            }
                        }
                        else
                        {
                            if(currentMinutes.toString().length==1)
                            {
                                (minutes).text('0'+currentMinutes);
                            }
                            else
                            {
                                (minutes).text(currentMinutes);
                            }
                        }
                    }
                    else
                    {
                        if(currentSeconds.toString().length==1)
                        {
                            seconds.text('0'+currentSeconds);
                        }
                        else
                        {
                            seconds.text(currentSeconds);
                        }
                    }   
                }

                return this.each(function() 
                {
                    console.log(_this);
                    setInterval("this.tick()",1000);
                });
            }
        });

        })(jQuery);

        $(document).ready(function()
        {
            $("#timer1").countdown();
            $("#timer2").countdown();
            $("#timer3").countdown();
        });

    </script>


</head>

<body>

    <div id="timer1">
        <span class="days">1</span>
        <span class="hours">18</span>
        <span class="minutes">6</span>
        <span class="seconds">45</span>
    </div>

    <div id="timer2">
        <span class="days">2</span>
        <span class="hours">28</span>
        <span class="minutes">1</span>
        <span class="seconds">59</span>
    </div>

    <div id="timer3">
        <span class="days">10</span>
        <span class="hours">0</span>
        <span class="minutes">59</span>
        <span class="seconds">59</span>
    </div>


</body>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:56:36+00:00Added an answer on May 23, 2026 at 5:56 pm

    I edited your code

    (function($){
                $.fn.extend({
    
                    countdown: function(options) {
    
                    var defaults = {
                        daysSelector : 'span.days',
                        hoursSelector : 'span.hours',
                        minutesSelector : 'span.minutes',
                        secondsSelector : 'span.seconds'
                    }
    
                    var options =  $.extend(defaults, options);
                    var _this  = $(this);
    
                   var tick =  function()
                    {
                        var    days = _this.find(options.daysSelector);
                        var    hours = _this.find(options.hoursSelector);
                        var    minutes = _this.find(options.minutesSelector);
                        var    seconds = _this.find(options.secondsSelector);
                        console.log(_this);
                        var currentSeconds=seconds.text();
                        currentSeconds--;
                        if(currentSeconds<0)
                        {
                            seconds.text("59");
                            var currentMinutes=minutes.text();
                            currentMinutes--;
                            if(currentMinutes<0)
                            {
                                (minutes).text("59");
                                var currentHours=(hours).text();
                                currentHours--;
                                if(currentHours<0)
                                {
                                    (hours).text("23");    
                                    var currentDays=(hours).text();
                                    currentDays--;
                                }
                                else
                                {
                                    if(currentHours.toString().length==1)
                                    {
                                        (hours).text('0'+currentHours);
                                    }
                                    else
                                    {
                                        (hours).text(currentHours);
                                    }
                                }
                            }
                            else
                            {
                                if(currentMinutes.toString().length==1)
                                {
                                    (minutes).text('0'+currentMinutes);
                                }
                                else
                                {
                                    (minutes).text(currentMinutes);
                                }
                            }
                        }
                        else
                        {
                            if(currentSeconds.toString().length==1)
                            {
                                seconds.text('0'+currentSeconds);
                            }
                            else
                            {
                                seconds.text(currentSeconds);
                            }
                        }    
                    }
    
                    return _this.each(function() 
                    {
                        console.log(_this);
                        setInterval(tick,1000);
                    });
                }
            });
    
            })(jQuery);
    
            $(document).ready(function()
            {
                $("#timer1").countdown();
                $("#timer2").countdown();
                $("#timer3").countdown();
            });
    

    tick was global and that was the problem (also never pass code to be evalued to setInterval!

    Fiddle here: http://jsfiddle.net/kvqWR/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are having problem with the server migration. We have one application that are
I'm having problem with Rails plugin attachment_fu . On every upload, I get validation
I having problem to update the counter (integer value). this is the definitions of
I having problem with append() when the textbox is focus the second time. var
having problem with null pointer exception and i read few article bout that error
Having problem to open form in new window, script is pulled from external service.
i having problem understanding what is wrong with my inheritance. I just can not
I am having problem understanding Priority Inversion Snippet from the article: Consider there is
Im having problem in importing hotmail contacts from hotmail in php it simple give
I'm having problem with listening to an event dispatched from a child class, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.