Javascript 单击后 Jquery 延迟

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

Jquery Delay After Click

javascriptjqueryhtml

提问by Aletz Morgan

I'm creating a web app...

我正在创建一个网络应用程序...

i got a kind of game, my idea is when the user completes each level appears a dialog box with some information, it is fine.

我有一种游戏,我的想法是当用户完成每个级别时会出现一个包含一些信息的对话框,这很好。

Now my issue is i want to show this message 5 seconds after the user clicks on the finish button.

现在我的问题是我想在用户单击完成按钮 5 秒后显示此消息。

thats my code:

那是我的代码:

$('#option-main-menu').click(function(){
        target.append('\
            <div id="confirm">\
                <h1>Are You Sure Want to Exist?</h1>\
                <a href="#" id="dialog-confirm">Yes</a><a href="#" id="dialog-cancel">No</a>\
            </div>\
        ');
    });

also i tryed with append().Delay(10000)but does not work.

我也试过append().Delay(10000)但不起作用。

Thanks in advance.

提前致谢。

回答by Tim Medora

Use setTimeout()with a delay of 5000 ms.

使用setTimeout()5000 毫秒的延迟。

$("button").click(
    function() {
        console.log("clicked...waiting...");

        setTimeout(
            function() {
                alert("Called after delay.");
            },
            5000);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button type="button">Click Me</button>

Just out of curiosity, why would you want to wait 5 seconds before prompting the user in response to an action? That's a long time; long enough for them to have clicked on a bunch of other things (if nothing else).

出于好奇,为什么要在提示用户响应某个操作之前等待 5 秒钟?那是很长的时间;足够长的时间让他们点击一堆其他东西(如果没有别的)。

回答by Purag

You could try using setTimeout().

您可以尝试使用setTimeout().

Example.

例子

setTimeout(function(){
    // do stuff here, in your case, append text
}, 5000);

The 5000can be replaced with any value of time that determines the length of the delay in milliseconds.

5000可以与确定以毫秒为单位的延迟的长度的时间的任何值来代替。

回答by James Montagne

You can't delay an append. You can only delay animations. You could use setTimeoutor show the element initially hidden and then fade it in:

你不能延迟追加。您只能延迟动画。您可以使用setTimeout或显示最初隐藏的元素,然后将其淡入:

$('<div id="confirm" style="display:none"> ...')
    .appendTo(target)
    .delay(5000)
    .fadeIn(1000);

回答by Leo Chan

try:

尝试:

$('#option-main-menu').click(function(){
    setTimeout(
        function(){
         target.append('\
            <div id="confirm">\
                <h1>Are You Sure Want to Exist?</h1>\
                <a href="#" id="dialog-confirm">Yes</a><a href="#" id="dialog-cancel">No</a>\
            </div>\
        ');
       }
        , 5000);
   });

回答by Dinesh

$("button").click(
    function() {
        console.log("clicked...waiting...");

        setTimeout(
            function() {
                alert("Called after delay.");
            },
            5000);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button type="button">Click Me</button>

回答by Avinash Kumar Anshu

This will work

这将工作

$("button").click(
function() {
    console.log("clicked...waiting...");

    setTimeout(
        function() {
            alert("Called after delay.");
        },
        5000);
});

回答by DA.

$yourThing.click(function(){
    setTimeout(function(){do your thing here},5000)
})