• Set time using jQuery

    Set Time using Jquery

    For Set Time using Jquery you have to go through the steps which are described below.

    Step 1 : First you have to create timedemo.html file and copy code of that file from below.
    Step 2 : Now you have to create jscounter.js file and copy code of that file from below.
    Step 3 : and now you have to download jquery.js file library of jquery from this url http://jquery.com/download/.

    Code :
    File : Example like “timedemo.html”

    1
    
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /><script type="text/javascript" src="jquery.js" language="JavaScript"></script><script type="text/javascript" src="JSCounter.js" language="JavaScript"></script>

    Fixed time with showing zero:

    Fixed time without showing zero:

    Auto increase time:

    Auto decrease time:

    File : Example like “jscounter.js”

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    
    /**
     * @fileOverview This file is a source which be used to create a counter
     * @author <a href="mailto:fogtower1979@gmail.com">Fog Tower</a>
     * @see <a href="http://www.fogtower.com">fogtower.com</a>
     * Version: 1.0.0
     */
     
     /**
     * Construct a namespace.
     * @namespace JSCounter
     */
    JSCounter = {
     
    };
     
    /**
     * Construct a new JSCounter.NumberCounter.
     * @class Represents a JSCounter.NumberCounter.
     * @constructor
     * @example
     * var myCounter = new JSCounter.NumberCounter("containerId");
     * @param {String} targetId The id of JSCounter.NumberCounter's container.
     * @param {Object} options The config parameters of JSCounter.NumberCounter.
     * @return A new instance of a JSCounter.NumberCounter.
     */
    JSCounter.NumberCounter = function(container, options){
       /**
        * The id of JSCounter's container
        * @type String
        */
        this.container = container;
     
        /**
        * The config parameters of JSCounter
        * @type Object
        */
        this.options = $.extend({
     
            /**
            * Height of number
            * @type string
            * @default "30px"
            */
            numberHeight: "30px",
     
            /**
            * Width of number
            * @type string
            * @default "14px"
            */
            numberWidth:"14px",
     
            /**
            * Font Size of number
            * @type string
            * @default "25px"
            */
            numberSize:"25px",
     
            /**
            * Whether auto run
            * @type boolean
            * @default false
            */
            auto:false,
     
            /**
            * Whether auto increase
            * @type boolean
            * @default true
            */
            increase:true,
     
            /**
            * Step of auto increase or decrease
            * @type int
            * @default 1
            */
            step:1,
     
            /**
            * Duration of animation (in milliseconds)
            * @type int
            * @default 1000
            */
            speed:1000,
     
            /**
            * Time between the end of the scroll and the beginning of the animation in milliseconds
            * @type int
            * @default 1000
            */           
            delay:1000,
     
            /**
            * Default init number
            * @type int
            * @default 10000
            */
            start:10000,
     
            /**
            * The character in front of number
            * @type string
            * @default ""
            */
            pretoken:"",
     
            /**
            * Whether show the comma in the number
            * @type boolean
            * @default true
            */
            comma:true
        }, options || {});
     
    };
     
    /**
     * Render JSCounter.NumberCounter.
     * @public
     */
    JSCounter.NumberCounter.prototype.render = function(){
        if(this.options.start &lt; 0){
            this.minus = true;
            this._createDiv(-this.options.start);
        }else{
            this.minus = false;
            this._createDiv(this.options.start);
        }
        this.lastNumber = this.options.start;
        if(this.options.auto){
            this.run();
        }
    };
     
    /**
     * Run JSCounter.NumberCounter.
     * @public
     */
    JSCounter.NumberCounter.prototype.run = function(){
        if(this.running){
            return;
        }
        this.running = true;
        this._run();
    };
     
    /**
     * Auto run JSCounter.NumberCounter.
     * @private
     */
    JSCounter.NumberCounter.prototype._run = function(){
        if(this.options.increase){
            this.setNumber(this.lastNumber + this.options.step);
        }
        else{
            this.setNumber(this.lastNumber - this.options.step);
        }
        var othis = this;
        this.autoCounter = setTimeout(function(){othis._run();},this.options.delay);
    };
     
    /**
     * Stop auto running JSCounter.NumberCounter.
     * @public
     */
    JSCounter.NumberCounter.prototype.stop = function(){
        this.running = false;
        clearTimeout(this.autoCounter);
    };
     
    /**
     * Create JSCounter.NumberCounter elements in the container.
     * @private
     */
    JSCounter.NumberCounter.prototype._createDiv = function(number){
        var str = "";
        if(this.options.pretoken){
            str += "
    ” + this.options.pretoken + “
    1
    2
    3
    4
    
    ";
        }
     
        str += "
    -
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    ";
        var positions = this._getPosition(number);
        var arr = [];
        var commaCount = 0;
        for(var i = 0; i &lt; number.toString().length; i++){
            var div = "";
            if(this.options.comma){
                if(commaCount == 3){
                    var commaDiv = "
    ,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    ";
                    commaCount = 1;
                    arr.push(commaDiv);
                }
                else{
                    commaCount++;
                }
            }
            div = "
    ” + ”

    “; for(var j = 9; j > -2; j–){ if(j == -1){ div += ”

    ” }else{ div += ”

    ” + j + “

    ” } } div += “

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    
    ";
             arr.push(div);
        }
        str += arr.reverse().join("");
        document.getElementById(this.container).innerHTML = str;
    };
     
    /**
     * Get the bottom's value of number.
     * @param {Number} number The number which will get the positions.
     * @private
     * @returns {Array} Every number's bottom value.
     */
    JSCounter.NumberCounter.prototype._getPosition = function(number){
        var numbers = number.toString();
        var position = [];
        for(var i = numbers.length - 1; i &gt; -1; i--){
            var numberPos = (9 - parseInt(numbers.charAt(i), 10)) * parseInt(this.options.numberHeight,10);
            position.push(numberPos);
        }
        return position;
    };
     
    /**
     * Set number to show.
     * @param {Number} number The number which will be set.
     * @public
     */
    JSCounter.NumberCounter.prototype.setNumber = function(number){
        if(typeof number != "number"){
            alert("The input parameter is not a number.");
            return;
        }
        var minusDiv = document.getElementById(this.container + "-minus");
        var myNumber = number;
        if(number &lt; 0){
            this.minus = true;
            myNumber = -number;
            minusDiv.style.display = "";
        }else{
            this.minus = false;
            minusDiv.style.display = "none";
        }
        if(myNumber.toString().length != this.lastNumber.toString().length){
            var str = "";
            for(var i = 0; i &lt; myNumber.toString().length; i++){
                if(i == 0){
                    str += "1";
                }else{
                    if(this.options.increase){
                        str += "9";
                    }
                    else{
                        str += "0";
                    }
                }
            }
            this._createDiv(parseInt(str,10));
            this.lastNumber = parseInt(str,10);
        }
        var positions = this._getPosition(myNumber);
        var lastNumberLength = this.lastNumber.toString().length;
        for(var i = 0; i &lt; lastNumberLength; i++){
            var id = this.container + "-number-" + i;
            if(positions[i] || positions[i] == 0){
                $("#" + id).animate({bottom: positions[i]}, this.options.speed);
            }
            else{
                $("#" + id).animate({bottom: 10 * parseInt(this.options.numberHeight,10)}, this.options.speed);
            }
        }
        this.lastNumber = number;
    };
     
    /**
     * Construct a new TimeCounter.
     * @class Represents a TimeCounter.
     * @constructor
     * @example
     * var myCounter = new JSCounter("containerId");
     * @param {String} targetId The id of JSCounter's container.
     * @param {Object} options The config parameters of JSCounter.
     * @return A new instance of a JSCounter.
     */
    JSCounter.TimeCounter = function(container, options){
       /**
        * The id of JSCounter's container
        * @type String
        */
        this.container = container;
     
        /**
        * The config parameters of JSCounter
        * @type Object
        */
        this.options = $.extend({
     
            /**
            * Height of number
            * @type string
            * @default "30px"
            */
            numberHeight: "30px",
     
            /**
            * Width of number
            * @type string
            * @default "14px"
            */
            numberWidth:"14px",
     
            /**
            * Font Size of number
            * @type string
            * @default "25px"
            */
            numberSize:"25px",
     
            /**
            * Whether auto run
            * @type boolean
            * @default false
            */
            auto:false,
     
            /**
            * Whether auto increase
            * @type boolean
            * @default true
            */
            increase:true,
     
            /**
            * Step of auto increase or decrease
            * @type int
            * @default 1
            */
            step:1,
     
            /**
            * Duration of animation (in milliseconds)
            * @type int
            * @default 1000
            */
            speed:1000,
     
            /**
            * Time between the end of the scroll and the beginning of the animation in milliseconds
            * @type int
            * @default 1000
            */           
            delay:1000,
     
            /**
            * Default init number
            * @type int
            * @default 10000
            */
            initHour:20,
     
            /**
            * Default init number
            * @type int
            * @default 10000
            */
            initMinute:59,
     
            /**
            * The character in front of number
            * @type string
            * @default ""
            */
            initSecond:59,
     
            /**
            * Whether show zero
            * @type boolean
            * @default true
            */
            showZero:true,
     
            /**
            * Callbcak function when time is zero
            * @type boolean
            * @default true
            */
            finalFunction : null
        }, options || {});
     
    };
     
    /**
     * Render JSCounter.TimeCounter.
     * @public
     */
    JSCounter.TimeCounter.prototype.render = function(){
        this._initTime(this.options.initHour, this.options.initMinute, this.options.initSecond);
        this._createDiv(this.lastHour, this.lastMinute, this.lastSecond);
        if(this.options.auto){
            this.run();
        }
    };
     
    /**
     * Init original time.
     * @private
     */
    JSCounter.TimeCounter.prototype._initTime = function(hour, minute, second){
        if(hour == null || typeof hour =="undefined"){
            this.lastHour = null;
        }
        else if(typeof hour != "number" || hour &lt; 0){
            this.lastHour = 0;
        }
        else{
            this.lastHour = hour;
        }
     
        if(minute == null || typeof minute =="undefined"){
            if(this.lastHour == null){
                this.lastMinute = null;
            }
            else{
                this.lastMinute = 0;
            }
        }
        else if(typeof minute != "number" || minute &gt; 59 || minute &lt; 0)
        {
            this.lastMinute = 0;
        }
        else{
            this.lastMinute = minute;
        }
     
        if(typeof second != "number" || second &gt; 59 || second &lt; 0){
            this.lastSecond = 0;
        }
        else{
            this.lastSecond = second;
        }
     
    };
     
    /**
     * Run JSCounter.TimeCounter.
     * @public
     */
    JSCounter.TimeCounter.prototype.run = function(){
        if(this.running){
            return;
        }
        this.running = true;
        this._run();
    };
     
    /**
     * Auto run JSCounter.TimeCounter.
     * @private
     */
    JSCounter.TimeCounter.prototype._run = function(){
        var time = this._getTime();
        if(time == null){
            this.stop();
            if(typeof this.options.finalFunction == "function"){
                this.options.finalFunction();
            }
            return;
        }
        this.setTime(time.hour, time.minute, time.second);
        var othis = this;
        this.autoCounter = setTimeout(function(){othis._run();},this.options.delay);
    };
     
    /**
     * Get auto run time.
     * @private
     * @returns {Object} auto run time.
     */
    JSCounter.TimeCounter.prototype._getTime = function(){
        var time = {};
        var hour = this.lastHour;
        var minute = this.lastMinute;
        var second = this.lastSecond;
        if(this.options.increase){
            second++;
            if(second == 60){
                second = 0;
                minute++;
            }
            if(minute == 60){
                minute = 0;
                hour++;
            }
        }
        else{
            second--;
            if(second == -1){
                second = 59;
                minute--;
            }
            if(minute == -1){
                minute = 59;
                hour--;
            }
            if(hour &lt; 0){
                return null;
            }
        }
     
        time.second = second;
        time.minute = minute;
        time.hour = hour;
        return time;
    };
     
    /**
     * Stop auto running JSCounter.TimeCounter.
     * @public
     */
    JSCounter.TimeCounter.prototype.stop = function(){
        this.running = false;
        clearTimeout(this.autoCounter);
    };
     
    /**
     * Create JSCounter.TimeCounter elements in the container.
     * @private
     */
    JSCounter.TimeCounter.prototype._createDiv = function(hour, minute, second){
        var str = "";
        var housPositions = this._getPosition(hour);
        this.hourLength = housPositions.length;
        for(var i = 0; i &lt; housPositions.length; i++){
            str += "
    ” + ”

    “; for(var j = 9; j > -1; j–){ str += ”

    ” + j + “

    ” } str += “

    1
    2
    3
    4
    
    ";
        }
        if(str){
            str += "
    :
    1
    2
    3
    4
    5
    
    ";
        }
        var minutePositions = this._getPosition(minute);
        for(var i = 0; i &lt; minutePositions.length; i++){
            str += "
    ” + ”

    “; for(var j = 9; j > -1; j–){ str += ”

    ” + j + “

    ” } str += “

    1
    2
    3
    4
    
    ";
        }
        if(str){
            str += "
    :
    1
    2
    3
    4
    5
    
    ";
        }
        var secondPositions = this._getPosition(second);
        for(var i = 0; i &lt; secondPositions.length; i++){
            str += "
    ” + ”

    “; for(var j = 9; j > -1; j–){ str += ”

    ” + j + “

    ” } str += “

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    
    ";
        }
        document.getElementById(this.container).innerHTML = str;
    };
     
    /**
     * Get the bottom's value of number.
     * @param {Number} number The number which will get the positions.
     * @private
     * @returns {Array} Every number's bottom value.
     */
    JSCounter.TimeCounter.prototype._getPosition = function(number){
        var position = [];
        if(number == null){
            return position;
        }
        var numbers = number.toString();
        if(this.options.showZero &amp;&amp; numbers.length &lt; 2){
            numbers = "0" + numbers;
        }
        for(var i = 0; i &lt; numbers.length; i++){
            var numberPos = (9 - parseInt(numbers.charAt(i), 10)) * parseInt(this.options.numberHeight,10);
            position.push(numberPos);
        }
        return position;
    };
     
    /**
     * Set time to show.
     * @param {Number} hour The hour which will be set.
     * @param {Number} minute The minute which will be set.
     * @param {Number} second The second which will be set.
     * @public
     */
    JSCounter.TimeCounter.prototype.setTime = function(hour, minute, second){
        if(typeof hour != "number" || typeof minute != "number" || typeof second != "number"){
            alert("The input parameter is not a number.");
            return;
        }
        var lastTimeString = this.lastHour.toString() + this.lastMinute.toString() + this.lastSecond.toString();
        this._initTime(hour, minute, second);
        if(!this.options.showZero){
            var tempstr1 = this.lastHour.toString() + this.lastMinute.toString() + this.lastSecond.toString();
            if(tempstr1.length != lastTimeString.length){
                var tempHour = "";
                var tempMinute = 1;
                var tempSecond = 1;
                for(var i = 0; i &lt; this.lastHour.toString().length; i++){
                    if(i == 0){
                        tempHour += "1";
                    }else{
                        if(this.options.increase){
                            tempHour += "9";
                        }
                        else{
                            tempHour += "0";
                        }
                    }
                }
                tempHour = parseInt(tempHour,10);
                if(this.lastMinute &gt; 9){
                    tempMinute = 10
                }
                if(this.lastSecond &gt; 9){
                    tempSecond = 10
                }
                this._createDiv(tempHour, tempMinute, tempSecond);
            }
        }
        var hourPosition = this._getPosition(this.lastHour);
        if(this.hourLength &gt; hourPosition.length &amp;&amp; this.options.showZero){
            hourPosition = hourPosition.reverse();
            var count = 0;
            for(var i = this.hourLength - 1; i &gt; -1; i--){
                var id = this.container + "-hour-" + i;
                if(hourPosition[count]){
                    $("#" + id).animate({bottom: hourPosition[count]}, this.options.speed);
                }
                else{
                    $("#" + id).animate({bottom: 9 * parseInt(this.options.numberHeight,10)}, this.options.speed);
                }
                count++;
            }
        }else{
            for(var i = 0; i &lt; hourPosition.length; i++){
                var id = this.container + "-hour-" + i;
                $("#" + id).animate({bottom: hourPosition[i]}, this.options.speed);
            }
        }
        var minutePosition = this._getPosition(this.lastMinute);
        for(var i = 0; i &lt; minutePosition.length; i++){
            var id = this.container + "-minute-" + i;
            $("#" + id).animate({bottom: minutePosition[i]}, this.options.speed);
        }
        var secondPosition = this._getPosition(this.lastSecond);
        for(var i = 0; i &lt; secondPosition.length; i++){
            var id = this.container + "-second-" + i;
            $("#" + id).animate({bottom: secondPosition[i]}, this.options.speed);
        }
     
    };
    Tags:

Comments on this post

Leave a Reply

  • Security Code :


    nine − = 3