Remove disabled attribute for each option in

I have a choice with different parameters. I have set as disabled options that have been in use in the last 30 seconds. What I am trying to get is to re-enable the options when the criteria is no longer valid but WHEN the page is still open. The following code works for one parameter, but since it's inside .each (), it gets overwritten for each parameter and it doesn't work. I was thinking of a temporary solution to store the diff value by adding a class to an element and triggering a given timeout for elements with that class, but I think this kind of code is not the most efficient. Any idea how to get the same result with more efficient code (inside each loop maybe?).

$('#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));
    }
});

      

+3


source to share


2 answers


I had the same situation where the problem has a loop and I want to bind for each loop a callback loop for change and I found that they all only work for the last id and to solve it I did the following: parameter that invokes the callback to save the state:

var arr = ["id1" ,"id2", "id3"];
for(i=0; i<arr.length; i++){
  jQuery("#form_target_input").bind('change',function (id, e){
    yiel.fn.setCookie("form_targeting_"+id, true)
   }.bind(null, id));
 }

      



for your case:

setTimeout(function(element, e) {
   $(element).prop('disabled', false);
}.bind(null, this), (30000-diff));

      

+1


source


try changing the last part:



setTimeout(function() {
    $(this).prop('disabled', false);
}.bind(this), (30000-diff));

      

+1


source







All Articles