js重要函数
分类:
IT文章
•
2022-04-05 22:54:28
window.setTimeout(code,millisec) 方法用于在指定的毫秒数后调用函数或计算表达式。只执行 code 一次(比如某个界面是上左右的三个frame界面,右边这个界面要调用某个函数,但是参数必须有左边框架传过来,为了防止左边框架未加载完,故可调用此参数)
$(document).ready(function() {
$(window).bind("scroll", scrollTrigger);
//调用这个方法是防止左边frmsetLeft关键字框架未加载完
window.setTimeout(loadPage, 500);
});
function loadPage() {
var sizerIds = window.parent.frames['frmsetLeft'].window.getCheckSizerIds();
if(sizerIds == undefined) {
//调用这个方法是防止左边frmsetLeft关键字框架未加载完
window.setTimeout(loadPage, 500);
return ;
}
funCheckSizer(sizerIds);
};
View Code
<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed alertbox!"
onClick="timedMsg()">
</form>
<p>Click on the button above. An alert box will be
displayed after 5 seconds.</p>
</body>
</html>
View Code
10秒后再执行某个程序
<SCRIPT LANGUAGE="JavaScript">
function test() {
//100毫秒后执行sleep方法, 但这10秒程序不会等待,会继续执行setTimeout后面的代码
//效果就是 先alert(2); 后 alert(1);
window.setTimeout("sleep()", 10000);
alert(2);
}
function sleep(){
alert(1);
}
test();
</SCRIPT>
View Code
定时器
<SCRIPT LANGUAGE="JavaScript">
var timer;//声明一个定时器
var count = 0;
function test()
{
//每隔500毫秒执行一次add()方法
timer = window.setInterval("add()",500);
}
function add(){
alert(++ count);
if (count == 5)
{
//如果count值达到5,清空定时器
window.clearInterval(timer);
}
}
test();
</SCRIPT>
View Code
window.setInterval(code,millisec) 方法用于可按照指定的周期(以毫秒计)来调用函数或计算表达式,返回值:一个可以传递给 Window.clearInterval() 从而取消对 code 的周期性执行的值。
<html>
<body>
<input type="text" id="clock" size="35" />
<script language=javascript>
var int=self.setInterval("clock()",50)
function clock()
{
var t=new Date()
document.getElementById("clock").value=t
}
</script>
</form>
<button onclick="int=window.clearInterval(int)">
Stop interval</button>
</body>
</html>
View Code
发现一点bug:
timer = window.setInterval("obtainprocess('"+packageId+"','"+filename+"','" + <%=resultFileType%>+"')",6000);
console.log("开启定时器,id是: " + timer);
如上面的代码timer是一个定义在外面的全局变量,每一条数据都需要一个定时器定时访问,
然而打印时console.log("开启定时器,id是: " + timer);这一句timer的值是不同的,但是当我清除定时器的时候window.clearInterval(timer);timer一直是第一次出现的值!
导致后面的定时器一直清除不掉,timer一直没变了