js定时的几种方式及插件变量公用(直接使用jsp中js变量)
2017-04-17 15:54
423 查看
js定时的几种方式
1,function show_tip(){
getTipData();
setTimeout("show_tip()",3000);
}
2,直接用jquery插件:
jsp中的上下文等要用到jsp专属的内置对象的比如(${pageContext.request.contextPath}),
只能在jsp中识别得到,但是类似插件的js文件需要用这种的话可以把以上要用的作为变量声明在jsp页面的全局js中,
这个js中再加载js插件,是js插件的代码就相当在jsp中写的,当然可以直接用这个全局变量(变量在哪些脚本前面,这些脚本可直接用)
如
jsp中:
weburl变量jquery.message.offer.js中可以直接用
<script>
var webUrl = "${pageContext.request.contextPath}";
$.getScript("${pageContext.request.contextPath}/resources/js/message/jquery.timer.js",////定时到这里即可
function() {
$.getScript("${pageContext.request.contextPath}/resources/js/message/jquery.message.offer.js",//自定义插件函数
function() {$.getScript("${pageContext.request.contextPath}/resources/js/message/jquery.form.js",//表单插件
function() {
$(".btnhr30").on('click',
function() {
checkCanBuy();
});
/* 每3秒取一次数据 */
timermessageRecord = $.timer(3000,
function() {
var count=infoCount(0);
//alert(count);
if(count>0){
$("#button_messageRecord").val("消息("+count+")");
$("#button_messageRecord").fadeOut(100).fadeIn(100);////////////闪烁效果
}
});
$(".class_customerNameSet").on('click',
function(){
switchCustomer(this);
}
);
//$(".class_customerNameSet").eq(0).click();
//checkmessageRecord(1);
});
});
});
</script>
jquery.timer.js:
/**
* jQuery Timer Plugin (jquery.timer.js)
* @version 1.0.1
* @author James Brooks <jbrooksuk@me.com>
* @website http://james.brooks.so
* @license MIT - http://jbrooksuk.mit-license.org
*/
(function($) {
jQuery.timer = function(interval, callback, options) {
// Create options for the default reset value
var options = jQuery.extend({ reset: 500 }, options);
var interval = interval || options.reset;
if(!callback) { return false; }
var Timer = function(interval, callback, disabled) {
// Only used by internal code to call the callback
this.internalCallback = function() { callback(self); };
// Clears any timers
this.stop = function() {
if(this.state === 1 && this.id) {
clearInterval(self.id);
this.state = 0;
return true;
}
return false;
};
// Resets timers to a new time
this.reset = function(time) {
if(self.id) { clearInterval(self.id); }
var time = time || options.reset;
this.id = setInterval($.proxy(this.internalCallback, this), time);
this.state = 1;
return true;
};
// Pause a timer.
this.pause = function() {
if(self.id && this.state === 1) {
clearInterval(this.id);
this.state = 2;
return true;
}
return false;
};
// Resumes a paused timer.
this.resume = function() {
if(this.state === 2) {
this.state = 1;
this.id = setInterval($.proxy(this.internalCallback, this), this.interval);
return true;
}
return false;
};
// Set the interval time again
this.interval = interval;
// Set the timer, if enabled
if (!disabled) {
this.id = setInterval($.proxy(this.internalCallback, this), this.interval);
this.state = 1;
}
var self = this;
};
// Create a new timer object
return new Timer(interval, callback, options.disabled);
};
})(jQuery);
1,function show_tip(){
getTipData();
setTimeout("show_tip()",3000);
}
2,直接用jquery插件:
jsp中的上下文等要用到jsp专属的内置对象的比如(${pageContext.request.contextPath}),
只能在jsp中识别得到,但是类似插件的js文件需要用这种的话可以把以上要用的作为变量声明在jsp页面的全局js中,
这个js中再加载js插件,是js插件的代码就相当在jsp中写的,当然可以直接用这个全局变量(变量在哪些脚本前面,这些脚本可直接用)
如
jsp中:
weburl变量jquery.message.offer.js中可以直接用
<script>
var webUrl = "${pageContext.request.contextPath}";
$.getScript("${pageContext.request.contextPath}/resources/js/message/jquery.timer.js",////定时到这里即可
function() {
$.getScript("${pageContext.request.contextPath}/resources/js/message/jquery.message.offer.js",//自定义插件函数
function() {$.getScript("${pageContext.request.contextPath}/resources/js/message/jquery.form.js",//表单插件
function() {
$(".btnhr30").on('click',
function() {
checkCanBuy();
});
/* 每3秒取一次数据 */
timermessageRecord = $.timer(3000,
function() {
var count=infoCount(0);
//alert(count);
if(count>0){
$("#button_messageRecord").val("消息("+count+")");
$("#button_messageRecord").fadeOut(100).fadeIn(100);////////////闪烁效果
}
});
$(".class_customerNameSet").on('click',
function(){
switchCustomer(this);
}
);
//$(".class_customerNameSet").eq(0).click();
//checkmessageRecord(1);
});
});
});
</script>
jquery.timer.js:
/**
* jQuery Timer Plugin (jquery.timer.js)
* @version 1.0.1
* @author James Brooks <jbrooksuk@me.com>
* @website http://james.brooks.so
* @license MIT - http://jbrooksuk.mit-license.org
*/
(function($) {
jQuery.timer = function(interval, callback, options) {
// Create options for the default reset value
var options = jQuery.extend({ reset: 500 }, options);
var interval = interval || options.reset;
if(!callback) { return false; }
var Timer = function(interval, callback, disabled) {
// Only used by internal code to call the callback
this.internalCallback = function() { callback(self); };
// Clears any timers
this.stop = function() {
if(this.state === 1 && this.id) {
clearInterval(self.id);
this.state = 0;
return true;
}
return false;
};
// Resets timers to a new time
this.reset = function(time) {
if(self.id) { clearInterval(self.id); }
var time = time || options.reset;
this.id = setInterval($.proxy(this.internalCallback, this), time);
this.state = 1;
return true;
};
// Pause a timer.
this.pause = function() {
if(self.id && this.state === 1) {
clearInterval(this.id);
this.state = 2;
return true;
}
return false;
};
// Resumes a paused timer.
this.resume = function() {
if(this.state === 2) {
this.state = 1;
this.id = setInterval($.proxy(this.internalCallback, this), this.interval);
return true;
}
return false;
};
// Set the interval time again
this.interval = interval;
// Set the timer, if enabled
if (!disabled) {
this.id = setInterval($.proxy(this.internalCallback, this), this.interval);
this.state = 1;
}
var self = this;
};
// Create a new timer object
return new Timer(interval, callback, options.disabled);
};
})(jQuery);
相关文章推荐
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
- 在jsp页面嵌入java代码和js并且在js中直接使用java代码中的变量
- 使用ajaxfileupload.js插件实现Ajax方式上传文件
- iOS开发 - OC - 实现本地数据存储的几种方式二(直接使用sqlite)
- JS定义和直接使用匿名函数的三种实现方式
- eclipse安装插件的方式 三种:links、eclipse中使用插件安装向导安装、直接copy插件到对应的eclipse目录 MyEclipse10安装SVN插件
- 【js】setTimeout几种使用方式
- myeclipse中的jsp检查未使用变量的设置方式
- 详解JS几种变量交换方式以及性能分析对比
- JS几种变量交换方式以及性能分析对比
- 打包公用的jsp,css,js到jar,其他war包引入后使用
- H5基础知识第八课时(JS使用方式与变量的声明)
- 使用Spring定时器定时任务的几种配置方式及示例
- jquery插件jquery.qrcode.js中使用二维码插件,中间放置logo图片的方式
- jsp下载的几种方式:1、运用struts的下载类DownloadAction 2、直接流化
- JQuery插件使用之Validation 快速完成表单验证的几种方式
- js中使用jsp声明变量方法 + js访问servlet及相互传值方法
- spring使用java配置定时任务的几种配置方式及示例
- JSP页面使用js直接打印页面信息加载水印
- JQuery插件使用之Validation 快速完成表单验证的几种方式