带有 slideStop 事件的 Bootstrap 滑块

Dri*_*ver 5 javascript jquery slider

我的页面上有一个引导滑块。我不知道如何更改此代码,在拖动滑块时 page.php 不会一直加载,而只有在我停止拖动它时(在所需的时间段之后)。可能我必须使用 slideStop 事件,但不知道如何使用。

<script type="text/javascript">
    $(document).ready(function() {
        var intSeconds = 1;
        var refreshId;

        function sTimeout() {
            $("#mydiv").load("page.php"); // load content
            refreshId = setTimeout(function() { // saving the timeout
                sTimeout();
            }, intSeconds *3000);
        }
        sTimeout();
        $.ajaxSetup({cache: false});

        // The slider
        $("#ex1").slider({
            min : 1, // minimum value
            max : 20, // maximum value
            step : 1,
            value : intSeconds, // copy current value
            formater: function(value) { // option to format the values before they are sent to the tooltip
                clearTimeout(refreshId); // clear it
                intSeconds = value; // update value
                sTimeout(); // restart it
                return value*3 + ' s';
            }
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

Rob*_*ker 4

好的,我会尝试一下。我猜你正在使用 bootstrap slider 插件/插件 https://github.com/seiyria/bootstrap-slider或类似的叉子。

所以你要做的就是首先取消setTimeoutonslideStart并恢复它 on slideStop。但是,如果 ajax 请求在滑块移动之前启动并在拖动期间返回,您也不想更新 div 的内容。

代码有点像这样:

JavaScript:

$(document).ready(function () {
    var intSeconds = 1;
    var refreshId;

    //set a flag so we know if we're sliding
    slideStart = false;
    $('#ex1').slider();
    $('#ex1').on('slideStart', function () {
        // Set a flag to indicate slide in progress
        slideStart = true;
        // Clear the timeout
        clearInterval(refreshId);
    });

    $('#ex1').on('slideStop', function () {
        // Set a flag to indicate slide not in progress
        slideStart = false;
        // start the timeout
        refreshId = setInterval(function () { // saving the timeout
            sTimeout();
        }, intSeconds * 3000);
    });

    //Change the sTimeout function to allow interception of div content replacement
    function sTimeout() {
        $.ajax({
            url: 'page.php',
            dataType: 'html',
            success: function (response) {
                if (slideStart) {
                    // slide in progress so bail out.
                    return;
                } else {
                    // slide not in progress so go ahead.
                    $("#mydiv").html(response);
                }
            },
            error: function () {
                // handle your error here
            }
        });
    }


    refreshId = setInterval(function () { // saving the timeout
        sTimeout();
    }, intSeconds * 3000);
});
Run Code Online (Sandbox Code Playgroud)