---
title: "JS 定时器"
author: "yang8820"
date: "2011-04-23T15:42:10Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2011-04-23T15:48:11Z"
id: 1013504
url: "https://yang8820.iteye.com/blog/1013504"
---

在JS中有两种定时器：
1、超时定时器setTimeout(function,time)，一般用于延迟一定时间后再进行某种操作（一次，即不会自动重复操作）。
2、循环定时器setInterval(function,time),常用于循环刷新数据操作（n次，即会自动重复操作）。
※清除已经存在的定时器对象：
clearTimeout(对象) 清除已存在的setTimeout对象
clearInterval(对象) 清除已存在的setInterval对象
※示例：
   var out=setTimeout(function(){alert("bye-bye!");},5000);
   var it=setInterval(function(){alert("等等!五秒后我还会来。");},5000);
清除定时器对象
   clearTimeout(out);
   clearInterval(it);