JavaScript怎么实现定时执行一个延时方法?

有这样一个需求:

<div id="demo"></div>


页面进入时这个元素是出现的,然后5s后元素消失,间隔8s后,元素出现,5s后元素再次消失,重复这个过程。

我的想法是这样的:


setInterval(function(){

    $('#demo').css('display','block')

    setTimeout(function(){

        $('#demo').css('display','none');

    },5000);

}, 8000);

但是时间貌似有问题,,是因为什么导致的呢?


慕妹3146593
浏览 411回答 5
5回答

喵喔喔

function delay() {&nbsp; &nbsp; setTimeout(function () {&nbsp; &nbsp; &nbsp; &nbsp; $('#demo').css('display','none');&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#demo').css('display','block');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delay();&nbsp; &nbsp; &nbsp; &nbsp; }, 8000);&nbsp; &nbsp; }, 5000)}delay();

开心每一天1111

把8000改为13000

元芳怎么了

css3 animation实现不是更好吗

吃鸡游戏

const a = function (){&nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; console.log('a');&nbsp; &nbsp; &nbsp; &nbsp; b()&nbsp; &nbsp; },1000);}const b = function (){&nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; console.log('b');&nbsp; &nbsp; &nbsp; &nbsp; a()&nbsp; &nbsp; },2000);}a()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript