我有一個用 jQuery 用 javascript 編寫的小型 Chrome 擴展程序,它應該從網頁中獲取一些數據。目標 HTML 如下所示:<div id="options"> <div class="option"><div class="checkbox">1</div></div> <div class="option"><div class="checkbox">3</div></div> <div class="option"><div class="checkbox">5</div></div> <div class="option"><div class="checkbox">Other value</div></div> ...</div><div id="result">0</div>當任何一個.checkbox被選中時#result都會改變。我的任務是.checkbox一項一項檢查并獲得#result每個。var result = {};// for each checkbox$('#options .option').each(function(){ // check it this.click() // wait a bit. Of course ideally if we can wait while #result is not changed but in practice sometimes checkbox doesn't change the result. So it will be enough to wait a few seconds // store data result[this.text()] = $('#result').text(); // uncheck the checkbox this.click() // wait});console.log(result);任何想法如何正確等待?謝謝UPD:一個顯示我的問題的小例子https://codepen.io/mihajlo-osadchij/pen/MNyazz
如何在 JavaScript 中“等待”
DIEA
2021-08-20 09:54:38