Dom(二)——定时器 & history

123 阅读1分钟

定时器

  1. n秒之后执行 setTimeout(function(){},ms)
  • 清除方法是clearTimeout(t1)(ps:t1是定时器名称)
  • setTimeout涉及到任务队列问题,当计时器设置的毫秒数是0时,并不会立刻执行,会先将他放入任务队列中,先执行主线程上的任务,主线程任务结束后,查看任务队列中的计时器,是否达到指定等待时间,如果达到那么就执行任务队列计时器的功能
setTimeout(function(){ // 异步的方法
      console.log(1)
    },0);
    console.log(2);
    console.log(3);
    <!--输出结果是2 3 1-->
    <!--setTimeout这种设置方法表示页面一打开就启动了定时器-->
  1. 每隔n秒之后执行一次的定时器setInterval(function(){},ms)
  • 清除定时器 clearInterval(t1)

history历史记录

  1. history.forward() 进入下一个页面
  2. history.go(1) 进入下一个页面
  3. history.back()返回上一个页面
  4. history.go(-1) 返回上一个页面
  5. history.go(number) 直接进入第几个页面

a标签跳转问题

  1. 不跳转
  • href="#"
  • href="javascript:void(0)"
<a href="#">1</a></br>
<a href="javascript:void(0)">2</a></br>
  1. 跳转新页面(原页面仍存在)
  <a href="https://www.baidu.com/" target="_blank">百度</a></br>