时钟插件 jQuery
Clock plugin jQuery
我正在编写一个 jquery 时钟插件,它工作正常,但我不能有超过 1 个页面时钟,旧版本可以正常工作,页面有无限数量的时钟,但不是 JQ 插件,所以有什么问题?
新代码:
(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 ) );
旧版本:
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);
};
}
提前谢谢..
你正在将你的JavaScript变量共享为全局变量,这些变量正在相互覆盖。
$.fn.newClock = function ( options ) {
var opt = $.extend({
ms: false,
type: 'classic'
}, options );
var element = $(this);
您正在共享变量t
、tNoMs
、hexTime
。而且,最重要的是,element
和opt
!我已将其隐藏在newClock()
方法中。所以这应该有效:
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
(function( $ ) {
$.fn.newClock = function ( options ) {
var t, tNoMs, hexTime, element; // here we making this variables local to current jQuery object
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;
}
// opt must be local too
var opt = $.extend({
ms: false,
type: 'classic'
}, options );
element = this; //you don't need here $(this)
var timer = 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 ) );
$(function() {
$('#c1').newClock({type: 'classic'});
$('#c2').newClock({type: 'hex'});
});
</script>
</head>
<body>
<div id="c1"></div>
<div id="c2"></div>
</body>
</html>
相关文章:
- 如何在Angular2中使用jQuery插件
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- 我的jQuery插件参数没有正确启动,遇到了问题
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- TableExport jquery插件:文件名和扩展名问题
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- jQuery插件-从插件中调用公共方法
- 扩展jQuery插件以更改配置
- "工具提示"jQuery插件坏了
- 不能在图像中的地图标记中使用花式框jquery插件的区域标记
- 使用Booklet Jquery插件,我如何验证最后一页是否显示
- 合并TinyMCE(jQuery插件)文件
- 带有jquery插件的backbone.js视图
- 自制jQuery插件已触发,但无法工作
- 我如何才能找到哪些网站使用我的jQuery插件
- jquery插件或javascript方法自动调整文本输入(而非文本区域)(固定宽度)可变高度的大小
- 在多个元素上使用jquery插件,只需稍作修改
- HtmlBox(jquery插件)在ajax调用中不起作用
- 构造jQuery插件
- 使用filedownload jquery插件处理FilePathResult返回值