当遇到setInterval,setTimeout与jquery混用的问题 时,直接按JavaScript中的语法写并不起作用,有以下两种解决方法。
方法1. 直接在ready中调用其他方法,会提示缺少对象的错误,应用jQuery的扩展可以解决这个问题。
$(document).ready(function(){
$.extend({
show:function(){
alert("ready");
}
});
setInterval("$.show()",3000);
});
方法2. 指定定时执行的函数时不要使用引号和括号。
function show(){
alert("ready");
}
setInterval(show,3000);// 注意函数名没有引号和括弧!
-------------------
setTimeout(表达式,延迟时间); 单位:ms(毫秒);1s=1000ms;
setInterval(表达式,交 互时间); 单位:ms(毫秒);1s=1000ms;
window.setTimeout()
在执行时,它从载入后延迟指定的时间去执行一个表达式或者是函数;仅执行一次;和window.clearTimeout一起使用.
window.setInterval()
在执行时,它从载入页面后每隔指定的时间执行一个表达式或者是函数;(功能类似于递归函数);和window.clearInterval一起使用.
function showTitle(str){
$('#title').html(str);
$('#title').slideDown();
}
for (var i=9;i>0;i--){
setTimeout("showTitle("+(10-i)+")",i*1000);
}
var count = 10;
function showTitle2(){
$('#interval').html(count);
$('#interval').slideDown();
count--;
if(count==8){
clearInterval(st);
}
}
var st = setInterval("showTitle2()",1000);
方法1. 直接在ready中调用其他方法,会提示缺少对象的错误,应用jQuery的扩展可以解决这个问题。
$(document).ready(function(){
$.extend({
show:function(){
alert("ready");
}
});
setInterval("$.show()",3000);
});
方法2. 指定定时执行的函数时不要使用引号和括号。
function show(){
alert("ready");
}
setInterval(show,3000);// 注意函数名没有引号和括弧!
-------------------
setTimeout(表达式,延迟时间); 单位:ms(毫秒);1s=1000ms;
setInterval(表达式,交 互时间); 单位:ms(毫秒);1s=1000ms;
window.setTimeout()
在执行时,它从载入后延迟指定的时间去执行一个表达式或者是函数;仅执行一次;和window.clearTimeout一起使用.
window.setInterval()
在执行时,它从载入页面后每隔指定的时间执行一个表达式或者是函数;(功能类似于递归函数);和window.clearInterval一起使用.
function showTitle(str){
$('#title').html(str);
$('#title').slideDown();
}
for (var i=9;i>0;i--){
setTimeout("showTitle("+(10-i)+")",i*1000);
}
var count = 10;
function showTitle2(){
$('#interval').html(count);
$('#interval').slideDown();
count--;
if(count==8){
clearInterval(st);
}
}
var st = setInterval("showTitle2()",1000);