i'm coding a jquery clock plugin, it works correctly but i can't have more than 1 clock for page, the old version works correctly with infinite numbers of clock for page, but is not a JQ plugin, so whats wrong?
New code:
(function( $ ) {
function time() {
d = new Date();
day = {
h: d.getHours(),
m: d.getMinutes(),
s: d.getSeconds(),
ms: d.getMilliseconds()
};
function check(i) {
if (i < 10) {i = "0" + i;}
return i;
}
day.h = check(day.h);
day.m = check(day.m);
day.s = check(day.s);
t = day.h + ":" + day.m + ":" + day.s + ":" + day.ms;
tNoMs = day.h + ":" + day.m + ":" + day.s;
hexTime = '#' + day.h + '' + day.m + '' + day.s;
}
$.fn.newClock = function ( options ) {
opt = $.extend({
ms: false,
type: 'classic'
}, options );
element = $(this);
setInterval(function () {
time();
switch (opt.ms) {
case true:
time();
element.html(t);
break;
case false:
switch (opt.type) {
case 'classic':
time();
element.html(tNoMs);
break;
case 'hex':
time();
element.html(hexTime);
break;
}
break;
}
}, 1);
};
} ( jQuery ) );
Old version:
function Clock() {
function temp() {
d = new Date();
day = {
h: d.getHours(),
m: d.getMinutes(),
s: d.getSeconds(),
ms: d.getMilliseconds()
};
day.h = check(day.h);
day.m = check(day.m);
day.s = check(day.s);
day.ms = check(day.ms);
function check(i) {
if (i < 10) {i = "0" + i;}
return i;
}
t = day.h + ":" + day.m + ":" + day.s + ":" + day.ms;
tNoMs = day.h + ":" + day.m + ":" + day.s;
hexTime = '#' + day.h + '' + day.m + '' + day.s;
}
temp();
this.new = function (selector, tf, type) {
function getStatus(variable) {
if (variable === true) {
return variable;
} else {
return variable;
}
}
setInterval(function () {
temp();
if (tf === true) {
$(selector).html(t);
} else {
$(selector).html(tNoMs);
}
if (type === true) {
$(selector).html(hexTime);
}
}, 1);
};
}
Thanks in advance..