如何延迟 jQuery-ui 滑块事件中的函数

发布于 2024-09-18 18:02:17 字数 896 浏览 1 评论 0原文

我在页面上有几个 jQuery-ui 范围滑块,它们在表中添加和删除行,并在滑块滑动时实时显示它们的值。函数 showData 必须在完成之前检查并更新每个表行,并且在 showData 函数返回之前幻灯片事件似乎不会执行任何操作。这个表可能会很大(1000+行)

这是滑块的设置之一:

.slider({
    min: 0,
    max: 1250,
    step: 50,
    values: [0, 1250],
    range: true,
    slide: function (e, ui) {
        $('span.height').text(ui.values[0] +
                'mm - ' + ui.values[1] + 'mm ');
        showData('height', ui.values, 'range');
    }
});

我的问题是,对于速度较慢的计算机上的 IE 用户来说,当他们滑动滑块时,看不到任何变化,甚至滑块手柄位置也没有变化。直到一秒或更长时间之后。

我想做的是让 $('span.height').text(... 幻灯片函数的一部分运行,并且 ui 滑块句柄立即更新在正确的位置(即:鼠标下方) 然后,

如果 showData 函数在 300 毫秒后运行,但前提是在这段时间内没有再次触发滑动事件,那么

我只需将 showData 函数放在滑块的停止事件上,但这不是客户端的操作 。我在 jsfiddle 上发布

了一个工作版本: http://jsfiddle.net/vcgAU/1/

任何帮助将不胜感激

谢谢

I have several jQuery-ui range sliders on a page which add and remove rows in a table and show their values above the sliders, live as the sliders are slid. the function showData has to check and update every table row before it's done and the slide event doesn't seem to execute anything until the showData function has returned. This table could potentially be quite big (1000+ rows)

Here is one of the slider's settings:

.slider({
    min: 0,
    max: 1250,
    step: 50,
    values: [0, 1250],
    range: true,
    slide: function (e, ui) {
        $('span.height').text(ui.values[0] +
                'mm - ' + ui.values[1] + 'mm ');
        showData('height', ui.values, 'range');
    }
});

My problem is that for IE users on a slow computer, nothing is seen to have changed when they slide the slider, not even the slider handle position. Until a second or more later.

What I want to do is let the $('span.height').text(... part of the slide function run, and the ui slider handle to be updated in the right place (ie: under the mouse) right away.

Then if the showData function was run after say 300 milliseconds, but only if the slide event was not triggered again in that time, would be perfect.

I would just put the showData function on the slider's stop event, but that's not what the client wants.

I put up a working version on jsfiddle: http://jsfiddle.net/vcgAU/1/

Any help would be appreciated

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2024-09-25 18:02:17

由于您当前的回调看起来非常相似,您可以使它们通用并通过延迟函数运行它们,但它需要针对每种类型,因为它们影响不同的事物(高度,宽度,深度)。您可以为每种类型创建一个延迟函数,如下所示:

var timers = {};
function delayShowData(type, values) {
  clearTimeout(timers[type]);
  timers[type] = setTimeout(function() {
    $('span.' + type).text(values[0] + 'mm - ' + values[1] + 'mm');
    showData(type, values, 'range');
  }, 300);
}

然后在 slide 回调中,调用 this 函数,如下所示:

slide: function (e, ui) {
    delayShowData('height', ui.values);
}

您可以在此处测试工作演示。在此设置中,它会为您所做的更改类型存储一个计时器,因此每个滑块都有效,并且当在间隔内再次更改时,它会清除计时器并重置 300 毫秒计时器...所以仅将其保留 300 毫秒就会导致为此类型调用 showData()

Since your current callbacks look very similar, you can make them generic and run them through a delay function, it needs to be per type though since they affect different things (height, width, depth). You can create a delay function that's per type like this:

var timers = {};
function delayShowData(type, values) {
  clearTimeout(timers[type]);
  timers[type] = setTimeout(function() {
    $('span.' + type).text(values[0] + 'mm - ' + values[1] + 'mm');
    showData(type, values, 'range');
  }, 300);
}

Then in your slide callback, call this function instead, like this:

slide: function (e, ui) {
    delayShowData('height', ui.values);
}

You can test a working demo here. In this setup it stores a timer for the type of change you're making, so per-slider effectively, and when changing again inside the interval it's clearing the timer out resetting the 300ms timer...so only leaving it alone for 300ms will result in a showData() being called for this type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文