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. iOS将自己的框架更新到cocopods上

    第一步 把自己的框架更新到github 上,为了提交地址给他人下载.这里就不详细介绍如何把项目更新到github上了 第二步 这个时候我们的项目已经挂在github上了我们需要给本地的项目新建一个Po ...

  2. Java这些冷知识你知道吗?

    1)jvm有很多种,其实jvm是一个标准,sun做的那个叫hotspot,作者就是后来v8的作者lars bak,其他公司也做过jvm,其中做得比较好的有bea的jrockit,其他的包括ibm的r9 ...

  3. angularJS的一点小笔记

    AngularJS用法:-------------------------------------------------- AngularJS 指令: AngularJS 通过被称为 指令 的新属性 ...

  4. 关于QQ空间相册功能的构想与简单实现

    QQ空间上传照片对其可以分类,形成不同的相册,这对于用户体验来说是很不错的,如果用户只能上传不加以分类,那么用户体验会很差. 下面是自己关于相册功能实现的一些简单看法: 首先,是创建相册,可以用pan ...

  5. 25.Linux-Nor Flash驱动(详解)

    1.nor硬件介绍: 从原理图中我们能看到NOR FLASH有地址线,有数据线,它和我们的SDRAM接口相似,能直接读取数据,但是不能像SDRAM直接写入数据,需要有命令才行 1.1其中我们2440的 ...

  6. LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  7. ubuntu中python3.4安装pip

    这两天碰到在ubuntu中安装pip的问题. 第一种方法 用百度搜索了一下,基本上都是这个命令: sudo apt-get install python3-pip 但是,用这条命令下载速度特别慢. 第 ...

  8. centos6.5配置uwsgi与nginx支持django

    一.centos中升级python 1. > wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tgz # https://ww ...

  9. Leetcode题解(22)

    66. Plus One 题目 这题很简单,直接代码: class Solution { public: vector<int> plusOne(vector<int> &am ...

  10. Intervals

    Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...