noUiSlider custom formatting time with libLink

寵の児 提交于 2019-12-07 15:52:28

It runs twice because libLink needs to use the slider .val() method to update. Setting the formatting on the link solves your issue.

$(".slider").noUiSlider({
    start: [ 0 ],
    range: {
        'min': 0,
        'max': 24 * 60
    }      
});

$(".slider").Link("lower").to($("#time"), null, {
    to: minutesToHHMM,
    from: HHMMtoMinutes
});

Updated fiddle.

I looked at the documentation and it appeared to have the same problem, so maybe this is a bug? (and I just looked again and it's working now) Anyway here is a workaround that doesn't use the libLink script https://jsfiddle.net/q4s4xrL7/2/

$(document).ready(function () {
    var $time = $('#time'),
        $slider = $('.slider');

    $slider.noUiSlider({
        start: ["07:30"],
        range: {
            'min': 0,
                'max': 24 * 60 - 1 // and I've subtracted 1 to make the range stop at 23:29
        },
        format: {
            to: minutesToHHMM,
            from: HHMMtoMinutes
        }
    }).on({
        slide: updateInputValue
    });

    function minutesToHHMM(value) {
        value = Math.round(value);
        var hour = Math.floor(value / 60);
        var min = value - hour * 60;
        //console.log("value:" + value + "\thour: " + hour + "\tmin: " + min)
        return ("0" + hour).slice(-2) + ":" + ("0" + min).slice(-2);
    }

    function HHMMtoMinutes(value) {
        var split = value.toString().split(":");
        var hour = parseInt(split[0], 10) * 60;
        var min = parseInt(split[1], 10);
        //console.log("value: " + value + "\thour: " + hour + "\tmin " + min);
        return hour + min;
    }

    $time.change(function () {
        $slider.val($time.val());
    });

    function updateInputValue() {
        $time.val($slider.val());
    }

    // run when page first loads
    updateInputValue();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!