等待某些东西停止来触发功能

Wait for something to stop to trigger function

本文关键字:功能 等待      更新时间:2023-09-26

我使用的是我用狡猾的工具darsa创建的日期选择器。一切都是完美的,除了如果用户更改日期太快,JavaScript不会触发函数的正确日期。

有没有办法:

if (datepicker not active for x seconds) 

或者是否有一种方法可以创建一个变量,并仅在该变量在x时间内不更改时触发该函数?我需要给JS一些时间,所以它不会触发函数,直到用户是在他的目标日期。

下面是一些代码

当日期选择器的日期发生变化时,我调用loadDateMatches(),它将所有匹配加载到HTML中。但如果你在第1天到第5天之间快速改变,它可能会在第3天停止加载匹配。

我正在寻找一种方法来触发功能loadDateMatches(),直到有一段时间没有改变日期。

days.on('active', function (eventName) {
    activeDate= this.rel.activeItem;
    var logDate = new Date(d.getFullYear(), 0, activeDate + first + 1);
    var startTime = new Date(logDate.getFullYear(), logDate.getMonth(), logDate.getDate(), 0, 0, 0);
    DayBarConditions.startTime = startTime.getTime()/1000;
    var endTime = new Date(logDate.getFullYear(), logDate.getMonth(), logDate.getDate(), 23, 59, 59);
    DayBarConditions.endTime = endTime.getTime()/1000;
    if (typeof loadDateMatches == 'function') {
        loadDateMatches();
    }
});

尝试让日期选择器在延迟时调用函数,该函数首先检查设置的日期是否与更改时相同,然后加载信息,如果是这样。我相信下面的代码应该是功能性的,但是它没有经过测试。

days.on('active', function (eventName) {
    activeDate= this.rel.activeItem;
    // We have to put this in a separate function, so that it evaluates activeDate
    // when the date picker is changed, not when activateDelayed is called
    (function(activeDate) {
        //Activate the function after .5 seconds if date remains unchanged
        window.setTimeout(activateDelayed, 500, activeDate);
    })(activeDate);
};
function activateDelayed (oldDate) {
    activeDate = days.rel.activeItem;
    if (oldDate == activeDate) {
        var logDate = new Date(d.getFullYear(), 0, activeDate + first + 1);
        var startTime = new Date(logDate.getFullYear(), logDate.getMonth(), logDate.getDate(), 0, 0, 0);
        DayBarConditions.startTime = startTime.getTime()/1000;
        var endTime = new Date(logDate.getFullYear(), logDate.getMonth(), logDate.getDate(), 23, 59, 59);
        DayBarConditions.endTime = endTime.getTime()/1000;
        if (typeof loadDateMatches == 'function') {
            loadDateMatches();
        }
    }
});

您可以使用此代码,它跟踪执行loadDateMatches请求的数量。当它是第一个时,该函数立即执行,但请求计数器不会减少,直到冷却期过去。只有这样,计数器才会减少。当计数器为1时,可以添加另一个请求,但它只会在第一个冷却期结束时导致执行。在此冷却期间,任何更多的请求都不会改变任何东西——在冷却之后,最多有一个请求将等待执行:

var requests = 0;
days.on('active', function (eventName) {
    // ... your existing code, setting DayBarConditions properties, comes here.
    // ...
    if (typeof loadDateMatches == 'function') {
        // Keep track of number of requests            
        if (requests < 2) requests++;
        // Ignore this when there is currently a cool-down ongoing, and
        // another execution is already pending:
        if (requests == 2) return;
        (function loop() {
            loadDateMatches();
            setTimeout(function () {
                // Cool down has passed: repeat when new request is pending
                if (--requests) loop();
            }, 500);
        })();
    }
});

因此,这段代码不会延迟第一个请求,而是引入一个冷却期,在此期间,任何进一步的请求都被合并为一个请求,并且只有在冷却期结束时才会执行。

但是可能有更好的解决方案,这取决于您在loadDateMatches中运行的代码。