jQuery 设置函数执行前的等待时间

jQuery Set wait time before a function executes

本文关键字:等待时间 执行 设置 函数 jQuery      更新时间:2023-09-26

如何设置此函数的等待时间。

function UpdateMedicaitonHistory(data) {
     MedicaitonHistoryGrid();
//set a wait time to finish MedicaitonHistoryGrid() like for 3 seconds
// then execute the below code.
if ($("#MedHistoryGridSec").is(":visible")) {
            alert("yes we have grid");
      }
else{
     alert("No Grid");
    }
}
您可以使用

setTimeout

setTimeout(function()
{
     if($('#MedHistoryGridSec').is(':visible'))
         alert('yes');
     else
         alert('no');
}, 3000);

您可以将代码包装在回调函数中,并在三秒钟后使用 window.setTimeout 运行它:

var afterThreeSeconds = function() {
  if ($("#MedHistoryGridSec").is(":visible")) {
    alert("yes we have grid");
  }
  else{
    alert("No Grid");
  }
}
window.setTimeout(afterThreeSeconds, 3000);

您能否向用药历史网格函数添加一个参数,该函数包含一个函数,其中包含您希望在该函数成功后执行的代码,然后您可以在药物历史网格函数结束时调用该代码?