[javascript学习指南]javascript基于定时器实现进度条功能实例

更新时间:2024-02-24    来源:网页配色    手机版     字体:

【www.bbyears.com--网页配色】

本文实例讲述了javascript基于定时器实现进度条功能。分享给大家供大家参考,具体如下:

Javascript 中的定时器

window度一线下面的方法 window.setInterval() 启动定时器

1.setInterval(function(函数),time(每隔多少时间执行一次函数,单位是毫秒))

会重复执行某项操作

2.setTimeout 运用在延迟一段时间,再进行某项操作

setTimeout(function,time) ,setTimeout 不会重复!

停止定时器

setTimeoout 对应的是clearTimeout(对象)   清除已设置的setTiemout对象

setInterval 对应的是clearInterval(对象)  清除已经设置的setInterval对象

给出一个案例:





www.jb51.net js进度条

<script language="javascript">
function show()
{
  if(document.getElementById("inner").offsetWidth<1000)//offsetWidth实现的时候width是没有宽度的,而style.width实现的时候则有宽度(px)!
  {
    document.getElementById("inner").style.width=
    document.getElementById("inner").offsetWidth+20+"px";//每次执行show()函数的时候宽度都会加上20!
    console.log(document.getElementById("inner").style.width);//console 控制台 :可以在网页上看到width的变化(利用F12)
  }
  else
  {
    document.getElementById("inner").style.width=1000+"px";//如果大于1000px的话,只能将1000px赋值给border-width;!
    stop();//此时就应该执行停止定时器的函数!
  }
}
var timer;//定义在两个函数外面,因为两个函数都会用到!
function start()
{
 timeer = window.setInterval(show,200);//每隔200ms调用一次show函数
}
function stop()
{
  timer = window.clearInterval(timer);
}
</script>









运行效果:

运行程序的时候,网页上的进度条就会加载,加载的快慢与时间有关!

本文来源:http://www.bbyears.com/wangyezhizuo/138167.html

猜你感兴趣