简体   繁体   English

从输入框单击时不更新 setTimeout() 值

[英]Not Updating setTimeout() value on click from input box

Here is a JS slider, that is changing slides in every 2 second from value of这是一个 JS slider,它每 2 秒从值为

<input style="width:50px;" type="text" id="rrrr" value="2000">

but first time when HTML Render the slider takes value properly.但是第一次当 HTML 渲染 slider 时正确地取值。

But after HTML Render updating value但是在 HTML 渲染更新值之后

<input style="width:50px;" type="text" id="rrrr" value="2000">

is doesn't changing slider speed.不会改变 slider 速度。

i tried this but it not working..我试过这个但它不工作..

var iHandle = 0;
$(document).ready(function() {
    //
    // read initial value:
    //
    var delay = document.getElementById("rrrr").value;
    //
    // set initial interval:
    //
    iHandle = window.setInterval(function() {
       // console.log(delay);
        function next() 
    }, delay);
    //
    // change delay by user trigger (pressing on button):
    //
    $("#speedbutton").on("click", function () {
        //
        // clear previous setInterval() object if existent:
        //
        if(iHandle) {
          clearInterval(iHandle);           
        }
        //
        // read new value:
        //
        var delay = document.getElementById("rrrr").value;
        //
        // set new interval:
        //
        iHandle = window.setInterval(function() {
          console.log(delay);
          function next() 
         }, delay);
    });
});

My aim is to change slider speed after changing input box value and click on <button id="speedbutton">Change Speed</button> (Such as: 1000, 3000, 5000)我的目标是在更改输入框值后更改 slider 速度并单击<button id="speedbutton">Change Speed</button> (例如:1000、3000、5000)

My Slider Whole Code is:我的 Slider 整个代码是:

 $(document).ready(function () { $('.sp').first().addClass('active'); $('.sp').hide(); $('.active').show(); var t = 0; var delay = document.getElementById("rrrr").value; function next() { clearTimeout(t); if (document.visibilityState === 'visible') { $('.active').removeClass('active').addClass('oldActive'); if ($('.oldActive').is(':last-child')) { $('.sp').first().addClass('active'); } else { $('.oldActive').next().addClass('active'); } $('.oldActive').removeClass('oldActive'); $('.sp').fadeOut(); $('.active').fadeIn(); } t = setTimeout(next, delay) } function prev() { clearTimeout(t); if (document.visibilityState === 'visible') { $('.active').removeClass('active').addClass('oldActive'); if ($('.oldActive').is(':first-child')) { $('.sp').last().addClass('active'); } else { $('.oldActive').prev().addClass('active'); } $('.oldActive').removeClass('oldActive'); $('.sp').fadeOut(); $('.active').fadeIn(); } t = setTimeout(next, delay) } $('#button-next').click(next); $('#button-previous').click(prev); t = setTimeout(next, delay) });
 body{font-size:12px; font-family: 'Verdana';} #page-wrapper{margin: 0 auto;position: relative;width: 500px;} #slider-wrapper {width:300px; height:200px;} #slider {width:300px; height:200px; position:relative;}.sp {width:300px; height:200px; position:absolute; font-size:20px; color:#fff;} #nav {margin-top:20px; width:50%;} #button-previous {float:left; cursor:pointer;} #button-next {margin-left: 250px;important:cursor;pointer;}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <div id="page-wrapper"> <div id="slider-wrapper"> <div id="slider"> <div class="sp" style="background: blue;">1</div> <div class="sp" style="background: yellow;">2</div> <div class="sp" style="background: green;" >3</div> <div class="sp" style="background: red;">4</div> </div> </div> <div id="nav"></div> <div id="button-previous">prev</div> <div id="button-next">next</div> </div> <br> <input style="width:50px;" type="text" id="rrrr" value="2000"> <button id="speedbutton">Change Speed</button>

Simply put a click listener to your button, that get the new value of input, and change the global delay var like:只需在您的按钮上添加一个单击侦听器,即可获取输入的新值,然后更改全局延迟变量,如下所示:

$("#speedbutton").on("click", function(e) {
    let newDelay = $("#rrrr").val();
    
     if (!isNaN(newDelay)) {
          delay = newDelay;
     }
  
})

See below wokring snippet:请参阅下面的工作片段:

 $(document).ready(function() { $('.sp').first().addClass('active'); $('.sp').hide(); $('.active').show(); var t = 0; var delay = document.getElementById("rrrr").value; function next() { clearTimeout(t); if (document.visibilityState === 'visible') { $('.active').removeClass('active').addClass('oldActive'); if ($('.oldActive').is(':last-child')) { $('.sp').first().addClass('active'); } else { $('.oldActive').next().addClass('active'); } $('.oldActive').removeClass('oldActive'); $('.sp').fadeOut(); $('.active').fadeIn(); } t = setTimeout(next, delay) } function prev() { clearTimeout(t); if (document.visibilityState === 'visible') { $('.active').removeClass('active').addClass('oldActive'); if ($('.oldActive').is(':first-child')) { $('.sp').last().addClass('active'); } else { $('.oldActive').prev().addClass('active'); } $('.oldActive').removeClass('oldActive'); $('.sp').fadeOut(); $('.active').fadeIn(); } t = setTimeout(next, delay) } $('#button-next').click(next); $('#button-previous').click(prev); t = setTimeout(next, delay) $("#speedbutton").on("click", function(e) { let newDelay = $("#rrrr").val(); if (;isNaN(delay)) { delay = newDelay; } }) });
 body { font-size: 12px; font-family: 'Verdana'; } #page-wrapper { margin: 0 auto; position: relative; width: 500px; } #slider-wrapper { width: 300px; height: 200px; } #slider { width: 300px; height: 200px; position: relative; }.sp { width: 300px; height: 200px; position: absolute; font-size: 20px; color: #fff; } #nav { margin-top: 20px; width: 50%; } #button-previous { float: left; cursor: pointer; } #button-next { margin-left: 250px;important: cursor; pointer; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <input style="width:50px;" type="text" id="rrrr" value="2000"> <button id="speedbutton">Change Speed</button><br> <div id="page-wrapper"> <div id="slider-wrapper"> <div id="slider"> <div class="sp" style="background: blue;">1</div> <div class="sp" style="background: gray;">2</div> <div class="sp" style="background: green;">3</div> <div class="sp" style="background: red;">4</div> </div> </div> <div id="nav"></div> <div id="button-previous">prev</div> <div id="button-next">next</div> </div> <br>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM