【www.gdgbn.com--js教程】

延迟自动完成
需求:在文本输入框中,监听用户输入,实现自动完成功能。
弊端:用户每输入一个字符,都会产生一个ajax请求,如果用户连续输入了一长串内容,请求次数就很多,实际上,最后的那次,才是用户需要的。
代码与上面例子类似。

延迟滚动
需求:页面的广告,需要用户滚动到哪,就跟到哪。
弊端:用户滚动在底,触发了n次的让广告重新定位的函数。其实,只需要当用户停下的时候,才触发一次就足够了。
代码与1类似。

 

var changetab = function(){
var timeid = 0;
return function(tabid){
if(timeid){
cleartimeout(timeid);
timeid=0;
}
settimeout(function(){
//ajax do something
},500);
};
}();

看一个完整的实例

<%@ page language="c#" autoeventwireup="true" codebehind="settimeout.asp教程x.cs" inherits="网页特效.settimeout" %>





<script type="text/javascript">
var flyouttimer;
function mouseo教程utevent() {
//hide flyout after 1 second when the mouse move out of the flyout zone
flyouttimer = settimeout(hideflyout, 1000);
}
function mouseoverevent() {
//clear the timer when the mouse move over the flyout
cleartimeout(flyouttimer);
}
function hideflyout() {
document.getelementbyid("flyout").style.display = "none";
}
function showflyout() {
document.getelementbyid("flyout").style.display = "block";
mouseoutevent();
}
</script>




click me to show flyout

this is a flyout



本文来源:http://www.gdgbn.com/wangyezhizuo/28651/