简体   繁体   English

设置动作 JS 的时间间隔(Await、setTimeout 或 setinterval)

[英]Set time interval on actions JS (Await, setTimeout Or setinterval )

We have more than 30 inputs in our HTML file, (name,adresse,phone....) on diferent pages我们的 HTML 文件中有 30 多个输入,(姓名,地址,电话....)在不同的页面上

the script in the chrome extension autocomplete all inputs one after one, in one time, chrome扩展程序中的脚本一次又一次地自动完成所有输入,

My question is how to make a function whait for the action beforr.我的问题是如何为之前的行动制作一个 function 。 set the value of each input one after one but with interval of 500ms.一个接一个地设置每个输入的值,但间隔为 500 毫秒。

function setById(id,valeur ){
    setTimeout(()=>{
        var ev = new Event('input');
        if (document.getElementById(id)) {
        Elem = document.getElementById(id) ? document.getElementById(id) : null
        Elem.value =valeur;
        Elem.dispatchEvent(ev);


    }else{
        console.log('L"element ' + id  + ' introuvable, pour valeur ==> '+ valeur);
    }
    },500);
}


///////////////////////////////////////
// immatriculation 
const immatriculation = setById('immatriculation',matricule.replaceAll('-',''));
// codePostalGarage
const codePostalGarage = setById('codePostalGarage',setData__.station_cp_n);

// villeGarage
const villeGarage = setById('villeGarage',setData__.station_ville_n);

Thank you Codeurs,谢谢码农们,

Try something like this尝试这样的事情

 // dummy data const matricule = "xxx" const setData__ = { station_cp_n: "x", station_ville_n: "y" } // object from your statements const values = [ { 'immatriculation': matricule.replaceAll('-', '') }, { 'codePostalGarage': setData__.station_cp_n }, { 'villeGarage': setData__.station_ville_n } ]; cnt = 0; const setById = () => { const ev = new Event('input'); const [id, valeur] = Object.entries(values[cnt])[0]; // destruct the element if (document.getElementById(id)) { Elem = document.getElementById(id)? document.getElementById(id): null Elem.value = valeur; Elem.dispatchEvent(ev); } else { console.log('L´element ' + id + ' introuvable, pour valeur ==> ' + valeur); } cnt++; if (cnt < values.length) setTimeout(setById, 500); // stop if finished } setById(); // start it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM