PS:希望各路大神能够指点

setTimeout(function,time):单位时间内执行一次函数function,以后不执行;对应清除定时器方法为clearTimeout;

setInterval(function,time):单位时间内执行一次函数function,以后一直重复执行函数;对应清除定时器方法为clearInterval;

其中function为函数名,假设其函数名为AutoPlay,其中如果写成AutoPlay,则表示这个函数,写成AutoPlay()则表示函数执行后的结果。但是两种写法函数最终都会执行。

当然我们也可以通过函数可以调用自身的特性用setTimeout来创建一个永久循环的函数,那么它的效果似乎和setInterval一样了,但实际上在细微之处是有区别的

setTimeout:

eg1:
function Automatism(){
func1... //函数执行话费时间400ms
setTimeout(Automatism(), 300);
}
setTimeout(Automatism(), 600); //实际是:func1 400s,600ms后在执行400s

setInterval: 

eg2:
function Automatism(){
func1... //函数执行话费时间400ms
} setInterval(Automatism(), 600); //实际是:func1开始执行时已开始计时,每隔600ms执行一次

清除定时:

清除定时人人都会,但这次我在撸轮播的过程中却发现无论如何也清不掉,经过几次测试才发现是因为使用setTimeout调用了自身的缘故.如何eg1中例子,即当我们使用setTimeout来达到重复执行的时候clearTimeout似乎失效了(目前不知道原因,请高手指点啊)。

最后换用setInterval可以正常工作。

最后贴上代码:

HTML:

<div class="companyBusiness" id="companyBusiness">
<dd id="album">
<img src="../images/companyBusiness.png" />
<img src="../images/companyBusiness.png" />
<img src="../images/companyBusiness.png" />
<img src="../images/companyBusiness.png" />
</dd>
<p id="circle">
<span class="on"></span>
<span></span>
<span></span>
<span></span>
</p>
</div>

CSS:

.companyBusiness{position: relative;width: 340px;height: 200px;overflow: hidden;display: inline-block;vertical-align: top;}
.companyBusiness dd{position: relative;display: inline-block;vertical-align: top;width: 1360px;transition: margin-left 0.8s ease-in-out;}
.companyBusiness dd img{width: 340px;height: auto;vertical-align: top;}
.companyBusiness p{position: absolute;bottom: 14px;right: 14px;}
.companyBusiness p span{display: inline-block;width: 7px;height: 7px;border-radius: 20px;background-color: #fff;margin-left: 5px;}
.companyBusiness p span.on{background-color: #da1622;}
.foucs_middle dt{width: 311px;display: inline-block;vertical-align: top;}
.foucs_middle dt img{width: 100%;height: auto;}

JS(清除定时器):

var timer = null;
$("#circle span").click(function(){
clearTimeout(timer);
var _thisIndex = $(this).index();
var dis = -(_thisIndex) * parseInt($("#companyBusiness").width());
$("#album").css("margin-left", dis);
$(this).addClass("on").siblings().removeClass("on");
timer = setTimeout("AutoPlay()", 3000);
});
function AutoPlay() {
var _thisIndex = $("#circle span.on").index();
_thisIndex++;
if (_thisIndex > 3) _thisIndex = 0;
$("#circle span").eq(_thisIndex).trigger("click");
setTimeout("AutoPlay()", 3000);
}
$(function() {
timer = setTimeout("AutoPlay()", 3000);
})

JS(不能清除定时器):

var timer = null;
$("#circle span").click(function(){
clearInterval(timer);
var _thisIndex = $(this).index();
var dis = -(_thisIndex) * parseInt($("#companyBusiness").width());
$("#album").css("margin-left", dis);
$(this).addClass("on").siblings().removeClass("on");
timer = setInterval("AutoPlay()", 3000);
});
function AutoPlay() {
var _thisIndex = $("#circle span.on").index();
_thisIndex++;
if (_thisIndex > 3) _thisIndex = 0;
$("#circle span").eq(_thisIndex).trigger("click");
}
$(function() {
timer = setInterval("AutoPlay()", 3000);
})

  

setTimeout和setInterval实现滚动轮播中,清除定时器的思考的更多相关文章

  1. H5滚动轮播插件

      概述 JRedu 随着前端技术的发展,越来越多的H5技术被应用到实际开发中,尤其是js插件更是无处不用,本章节我们主要分享下如何去自己封装一个滚动轮播插件. 1效果图如下: 2主要功能   支持超 ...

  2. bootstrap中的动态加载出来的图片轮播中的li标签中的class="active"的动态添加移除

    //该方法是在slide改变时立即触发该事件, $('#myCarousel').on('slide.bs.carousel', function () { $("#myCarousel o ...

  3. layui轮播中箭头不起作用问题

    layui轮播中箭头不起作用问题 layui轮播插件在使用中发现箭头不起作用其他都合适,是什么原因造成的呢?发现单独提出layui中的demo是合适的,通过仔细慢慢的寻找,发现layui.use('c ...

  4. iOS swift版本无限滚动轮播图

    之前写过oc版本的无限滚动轮播图,现在来一个swift版本全部使用snapKit布局,数字还是pageConrrol样式可选 enum typeStyle: Int { case pageContro ...

  5. 滚动轮播插件——jCarouselLite

    jcarousellite(上下.水平滚动元素插件)插件使用: 参数说明: btnPrev     string 上一个按钮的class名, 比如  btnPrev: ".prev" ...

  6. Android自动滚动 轮播循环的ViewPager

    主要介绍如何实现ViewPager自动播放,循环滚动的效果及使用.顺便解决ViewPager嵌套(ViewPager inside ViewPager)影响触摸滑动及ViewPager滑动速度设置问题 ...

  7. js原生选项卡(自动播放无缝滚动轮播图)二

    今天分享一下自动播放轮播图,自动播放轮播图是在昨天分享的轮播图的基础上添加了定时器,用定时器控制图片的自动切换,函数中首先封装一个方向的自动播放工能的小函数,这个函数中添加定时器,定时器中可以放向右走 ...

  8. bootstrap 获得轮播中的索引 getActiveIndex

    今天想用bootstrap做一个轮播,当轮播滚到每张图的时候,在页面下面就显示相对应的内容,那么问题来了:我肯定需要知道当前活动(显示图片)的索引号,那么bootstrap的轮播组件要怎么获得这个索引 ...

  9. JavaScript+HTML+CSS 无缝滚动轮播图的两种方式

    第一种方式 在轮播图最后添加第一张,一张重复的图片. 点击前一张,到了第一张,将父级oList移动到最后一张(也就是添加的重复的第一张),在进行后续动画. 点击下一张,到了最后一张(也就是添加的重复的 ...

随机推荐

  1. Python学习笔记(九)

    Python学习笔记(九): 装饰器(函数) 内置函数 1. 装饰器 1. 作用域 2. 高阶函数 3. 闭包 如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就 ...

  2. webstorm 卡死解决方法

    方法1: 先在外部终端清空node-modules目录,包括隐藏文件,再打开Webstorm,打开Project Structure页面,选中工程,选择node_modules目录(没有的话自己先新建 ...

  3. Android———最详细的系统对话框使用

    在实际应用开发中,用到系统对话框中的情况几乎是没有的.按开发流程来说,UI工程师都会给出每一个弹窗的样式,故而在实际开发中都是自定义弹窗的. 即使用到的地方不多,但是我们也是需要了解并且能熟练的运用它 ...

  4. LINUX 笔记-文件隐藏属性

    chmod u+s xxx 设置setuid(4775) chmod g+s xxx 设置gid(2775) chmod o+t xxx 设置stick bit,针对目录(1775)

  5. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  6. BZOJ-1225-[HNOI2001] 求正整数

    Description 对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m.例如:n=4,则m=6,因为6有4个不同整数因子1,2,3,6:而且是最小的有4个因子的整数. Input n ...

  7. 火狐浏览器打开html文件,中文乱码

    当html文件为: 解决方法: 将   <meta http-equiv="Content-Type" content="text/html; charset=UT ...

  8. session设置过期的方法(转载)

    这篇文章主要介绍了php中实现精确设置session过期时间的方法,需要的朋友可以参考下   大多数据情况下我们对于session过期时间使用的是默认设置的时间,而对于一些有特殊要求的情况下我们可以设 ...

  9. POJ3468(线段树 区间修改 lazy-tag)

    我的线段树真的没救了......还是多练几道吧....... You have N integers, A1, A2, ... , AN. You need to deal with two kind ...

  10. Paint the Grid Reloaded(缩点,DFS+BFS)

    Leo has a grid with N rows and M columns. All cells are painted with either black or white initially ...