如何每 1 秒单击一次按钮?

How can I click button every 1 seconds?

我已声明按钮:

<button type="button" class="btn btn-warning" style="top: -1px; position: relative; margin-left: 10px;" onclick="searchProduct()">Refresh / Search</button>

我应该在控制台中写什么才能每 1 秒单击该按钮?我找到了那个代码,但我不知道如何使用它:

setInterval(function () {document.getElementById("myButtonId").click();}, 1000);

按钮执行的是searchProduct()函数,直接执行即可。无需点击按钮

setInterval(function() {
  searchProduct();
}, 1000);

您需要将 id "myButtonId" 添加到您的 html 代码中。

setInterval(function () {
document.getElementById("myButtonId").click();
console.log('click');
}, 1000);
<button type="button" class="btn btn-warning" id="myButtonId" style="top: -1px; position: relative; margin-left: 10px;">
Refresh / Search
</button>