【发布时间】:2015-04-20 23:39:36
【问题描述】:
我有一个带有不同选项的选择。我将在过去 30 秒内使用过的选项设置为禁用。我想要得到的是当标准不再有效但页面仍然打开时再次启用选项。 以下代码适用于一个选项,但由于它位于 .each() 内部,因此每个选项都会被覆盖并且它不起作用。 我正在考虑一种解决方法来存储差异值,向元素添加一个类并在具有该类的项目上运行设置超时,但我认为这种代码不是最有效的。 关于如何使用更高效的代码(可能在每个循环内?)获得相同结果的任何想法。
$('#destinatario option').each(function(){
var ds = $(this).attr('data-ts');
console.log("ts vale "+ds);
var dateArray = ds.split(" "); // split the date and time
var ds1 = dateArray[0].split("-"); // split each parts in date
var ds2 = dateArray[1].split(":"); // split each parts in time
var newDate = new Date(ds1[0], (+ds1[1] - 1), ds1[2], ds2[0], ds2[1], ds2[2]).getTime(); //parse it
console.log("allora vale "+newDate);
var currentDate = new Date().getTime();
console.log("adesso vale "+currentDate);
var diff = currentDate - newDate;
console.log(diff);
if(diff < 30000){
$(this).prop('disabled', true);
setTimeout(function() {
$(this).prop('disabled', false);
}, (30000-diff));
}
});
【问题讨论】:
标签: javascript jquery