2016-09-14 59 views
1

我使用的是我用狡猾的工具darsa.in创建的日期选择器,除了如果用户改变日子过快之外,一切都是完美的,JavaScript不会为该功能触发正确的日期。等待一些东西停止触发功能

是否有这样做的方式:

if (datepicker not active for x seconds) 

还是有一种方式来创建一个变量,并触发只有在该变量不中×时间改变功能?我需要给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(); 
    } 
}); 
+0

你可以添加一些代码,让人们可以看到你想要做什么? – httpNick

+0

我加了它,但无论如何,我知道很难承认我正在弄的东西。我的英语并不完美对不起 –

+0

尝试使用datepicker附带的选择功能 - http://api.jqueryui.com/datepicker/#option-onSelect - 与close功能结合使用。有了这两个函数,你可以停止执行其他函数 – Tasos

回答

0

尝试具有日期选取呼吁,首先检查组当天是否相同,当它被改变,然后加载信息,如果这样的延迟的功能。我相信下面的代码应该是功能性的,但它没有经过测试。

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(); 
     } 
    } 
}); 
0

您可以使用此代码,它会跟踪你必须执行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中运行的代码,可能会有更好的解决方案。