我的jquery滑块刷新率不会实时更新

我正在尝试开发一个简单的jquery滑块,默认情况下每三秒钟在图像和文本之间切换滑块(var segundos = 3;),滑块有一个按钮,用作播放/暂停操作和菜单选项,它可以让你增加或减少每次在图像之间切换的秒数。 这是通过类.resta和.suma(减少和减少)完成的。

现在,滑块运行良好,但是有一个问题,当segundos变量(segundos表示秒)更改效果时,除非滑块暂停,否则它不会自动刷新,而滑块运行时不会自动刷新。 它必须是这样的:暂停滑块>更改默认秒>播放滑块>查看结果

我的目标是使其在滑块启动和运行时更改刷新率。

这是代码:

 $(document).ready(function(){ var segundos = 3000; var stop = true; var slideInterval; $('.play').click(function(){ if (stop == false) { stop = true; $('#playpause').text('Play it'); clearInterval(slideInterval); } else { stop = false; $('#playpause').text('Pause it'); slideInterval = setInterval(swapSlides, segundos); } }); function swapSlides(){ //var cs = $('div.currentslide:first'); var cs = $('#polaroid1').children('.currentslide:first'); var ns = cs.next(); if(ns.hasClass('mySlides1')){ cs.removeClass('currentslide'); ns.addClass('currentslide'); } else{ ns = $('#polaroid1').children('div.mySlides1:first'); cs.removeClass('currentslide'); ns.addClass('currentslide'); } } $('.resta').click(function(){ if(segundos > 1000){ segundos = segundos - 1000; segundoss = (segundos/1000); $('.segundos').text(" " + segundoss + "s "); } }); $('.suma').click(function(){ if(segundos >= 1000 && segundos < 15000){ segundos = segundos + 1000; segundoss = (segundos/1000); $('.segundos').text(" " + segundoss + "s "); } }); });  
 .menu{ list-style:none; bottom:8px; position:absolute; font-family: 'Covered By Your Grace', cursive; font-weight:300; width:200px; } .menu > li{ } .play{ width:59px; height:52px; position:absolute; left:5px; bottom:5px; cursor:pointer; z-index:10000; } .polaroid1{ box-shadow: 10px 10px 7px -7px rgba(0, 0, 0, 0.5); -webkit-backface-visibility: hidden; transform: rotate(-8deg); margin-bottom:30px; width:380px; height:320px; background-color:rgba(255, 255, 255, 1.0); text-align:right; padding-top:10px; padding-right:10px; padding-left:10px; padding-bottom:15px; top:15px; left:25px; position:relative; } .photo1{ width:100%; height:85%; position:relative; padding:5px; } .date1{ margin:0; padding-right:10px; font-family: 'Covered By Your Grace', cursive; transform: rotate(-5deg); font-size:28px; } .mySlides1{ display:none; width:380px; height:320px; position:absolute; top:0px; left:0px; } .currentslide{display:block;} 
  
Play it

22-05-2017

12-08-2017

22-05-2017

10-06-2017

19-08-2017

11-08-2017

22-05-2017

19-04-2017

20-05-2017

22-08-2017

05-07-2017

它必须是这样的:暂停滑块>更改默认秒>播放滑块>查看结果

我的目标是使其在滑块启动和运行时更改刷新率。

那是因为当你增加/减少滑动间隔时间(segundos)时,你从不对你的slideInterval做任何事情。

解:

添加函数以重新启动slideInterval如下所示:

  function _restartSlide() { clearInterval(slideInterval); slideInterval = setInterval(swapSlides, segundos); } 

jsfiddle链接https://jsfiddle.net/sudarpochong/2za1ccra/1/