簡體   English   中英

設置動作 JS 的時間間隔(Await、setTimeout 或 setinterval)

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

我們的 HTML 文件中有 30 多個輸入,(姓名,地址,電話....)在不同的頁面上

chrome擴展程序中的腳本一次又一次地自動完成所有輸入,

我的問題是如何為之前的行動制作一個 function 。 一個接一個地設置每個輸入的值,但間隔為 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);

謝謝碼農們,

嘗試這樣的事情

 // 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