如何在“X”秒后调用 jquery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8216394/
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-08-27 10:12:49  来源:igfitidea点击:

How to make a jquery function call after "X" seconds

jqueryfunctioncallsettimeout

提问by coder

I have a jquery function and I need to call it after opening the website in an Iframe.

我有一个 jquery 函数,我需要在 iframe 中打开网站后调用它。

I am trying to open a weblink in an Iframe and after opening it I need to call the below function. So how do I do that?

我试图在 Iframe 中打开一个网络链接,打开它后我需要调用下面的函数。那我该怎么做呢?

Here is my function:

这是我的功能:

<script type="text/javascript">
       $(document).ready(function(){
           $("#<%=Button1.ClientID%>").click(function (event) {

            $('#<%=TextBox1.ClientID%>').change(function () {
                $('#various3').attr('href', $(this).val());
            });
            $("#<%=Button2.ClientID%>").click();
        });
      })
    function showStickySuccessToast() {
        $().toastmessage('showToast', {
            text: 'Finished Processing!',
            sticky: false,
            position: 'middle-center',
            type: 'success',
            closeText: '',
            close: function () {

            }
        });
    }

    </script>

This is my button to open the link in an IFrame:

这是我在 IFrame 中打开链接的按钮:

<a id="various3" href="#"><asp:Button ID="Button1" 
runat="server" Text="Button" OnClientClick="Button2_Click"/></a>

Actually this is the simple Page I'm having:

实际上这是我拥有的简单页面:

enter image description here

在此处输入图片说明

And this is the message enter image description here

这是消息 在此处输入图片说明

回答by Layke

You can just use the normal setTimeout method in JavaScript.

你可以在 JavaScript 中使用普通的 setTimeout 方法。

ie...

IE...

setTimeout( function(){ 
    // Do something after 1 second 
  }  , 1000 );

In your example, you might want to use showStickySuccessToastdirectly.

在您的示例中,您可能希望showStickySuccessToast直接使用。

回答by zequinha-bsb

If you could show the actual page, we, possibly, could help you better.

如果您可以显示实际页面,我们可能会为您提供更好的帮助。

If you want to trigger the button only after the iframe is loaded, you might want to check if it has been loaded or use the iframe.onload:

如果您只想在 iframe 加载后触发按钮,您可能需要检查它是否已加载或使用 iframe.onload:

<iframe .... onload='buttonWhatever(); '></iframe>


<script type="text/javascript">

    function buttonWhatever() {
        $("#<%=Button1.ClientID%>").click(function (event) {
            $('#<%=TextBox1.ClientID%>').change(function () {
                $('#various3').attr('href', $(this).val());
            });
            $("#<%=Button2.ClientID%>").click();
        });

        function showStickySuccessToast() {
            $().toastmessage('showToast', {
                text: 'Finished Processing!',
                sticky: false,
                position: 'middle-center',
                type: 'success',
                closeText: '',
                close: function () { }
            });
        }
    }

</script>

回答by Love Kumar

try This

尝试这个

setTimeout( function(){ 
    // call after 5 second 
  }  , 5000 );