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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10320761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:27:33  来源:igfitidea点击:

jQuery Set wait time before a function executes

javascriptjquerytime-wait

提问by HaBo

How can i set wait time for this function.

如何设置此功能的等待时间。

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");
    }
}

回答by Tejs

You can use setTimeout:

您可以使用setTimeout

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

回答by rjz

You could wrap up the code in a callback function and run it after three seconds using window.setTimeout:

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

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

window.setTimeout(afterThreeSeconds, 3000);

回答by Rich Andrews

Can you add a parameter to the medication history grid function taking a function containing the code you want executed after that function succeeds which you can then call at the end of the medication history grid function?

您是否可以向用药史网格函数添加一个参数,其中包含一个包含要在该函数成功后执行的代码的函数,然后您可以在用药史网格函数的末尾调用该函数?