当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 使用自定义setTimeout和setInterval使之可以传递参数和对象参数

Javascript
Add a Table to a Word Document
Add Formatted Text to a Word Document
用jscript实现新建word文档
用jscript实现新建和保存一个word文档
Open and Print a Word Document
Use Word to Search for Files
Convert Seconds To Hours
Sample script that deletes a SQL Server database
Sample script that displays all of the users in a given SQL Server DB
firefox中用javascript实现鼠标位置的定位
div+css实现鼠标放上去,背景跟图片都会变化。
Locate a File Using a File Open Dialog Box
Save a File Using a File Save Dialog Box
用jscript实现列出安装的软件列表
List the Stored Procedures in a SQL Server database
Display SQL Server Login Mode
Display SQL Server Version Information
List all the Databases on a SQL Server
用jscript启动sqlserver
Stop SQL Server

Javascript 中的 使用自定义setTimeout和setInterval使之可以传递参数和对象参数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 384 ::
收藏到网摘: n/a

该函数兼容ie,firefox。并且可以使用clearSetTimeOut和clearInterval清除,比原setTimeout,setInterval方便很多,并且参数可以是object。 /******************************************************
//
// 功能: 修改window.setTimeout,使之可以传递参数和对象参数
// 使用方法: window.setTimeout(回调函数,延迟时间,参数1,参数n)
//
******************************************************/
var mySetTimeOut = setTimeout;
window.setTimeout = function(callback, timeout)
{
var args = Array.prototype.slice.call(arguments, 2);
function callFn(){callback.apply(null, args);}
return mySetTimeOut(callFn, timeout);
}
/******************************************************
//
// 功能: 修改window.setInterval,使之可以传递参数和对象参数
// 使用方法: window.setInterval(回调函数,间隔时间,参数1,参数n)
//
******************************************************/
var mySetInterval = setInterval;
window.setInterval = function(callback, interval)
{
var args = Array.prototype.slice.call(arguments, 2);
function callFn(){callback.apply(null, args);}
return mySetInterval(callFn, interval);
}
// 测试代码传递object
// 普通参数就不举例了
var obj = {height: 40px;}
var testTimeout = testInterval = null;
function test(obj)
{
alert(obj.height);
clearSetTimeOut(testTimeout);
clearInterval(testInterval);
}
var testTimeout = window.setTimeout(test, 100, obj);
var testInterval = window.setInterval(test, 100, obj);
该函数兼容ie,firefox。并且可以使用clearSetTimeOut和clearInterval清除,比原setTimeout,setInterval方便很多,并且参数可以是object。