How to avoid looping when trying to autocomplete input of type select using setInterval

0

I have the following code, the idea is that using interval select a value in a select and then assign a value to another select. But the change event that I should use is causing the page to refresh and loop. Any suggestions?

var interval = setInterval(doStuff, 3000); // 2000 ms = start after 2sec 

function doStuff() {
  chrome.storage.sync.get(null, 
    function(result) {
       var item = JSON.parse(result.key);
       var reg = document.getElementById('form1:layoutPanel1:layoutPanel2:regional_list');
       reg.value = item.regional;
       reg.dispatchEvent(new Event('change'));

       var ciudad = document.getElementById('form1:layoutPanel1:layoutPanel2:ciudad_list');
       ciudad.value = item.ciudad;


  });
  clearInterval(interval);
}
    
asked by Agustín Morelle 11.07.2018 в 01:06
source

1 answer

0

Taking the "debounce" function of underscore can be used as follows:

function debounce(fn, wait, inmediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!inmediate) fn.apply(context, args);
        };
        var callNow = inmediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) fn.apply(context, args);
   };
};

And then you invoke it as you need:

var waitFn = debounce(function() {
    /* Lógica de la función */
}, 1000); //tiempo en ms

This function can be stored in your JS file, you do not need to download the entire library to be able to execute it.

enter the code here

Greetings and I hope I have helped you.

    
answered by 12.07.2018 в 08:05