简体   繁体   English

单击后Jquery延迟

[英]Jquery Delay After Click

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. 提前致谢。

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? 出于好奇,你为什么要在提示用户响应某个动作之前等待5秒? That's a long time; 那是很长一段时间; long enough for them to have clicked on a bunch of other things (if nothing else). 足够长的时间让他们点击了其他一些东西(如果没有别的话)。

You could try using setTimeout() . 您可以尝试使用setTimeout()

Example . 例子

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

The 5000 can be replaced with any value of time that determines the length of the delay in milliseconds. 5000可以替换为确定延迟长度的任何时间值(以毫秒为单位)。

You can't delay an append. 你不能推迟追加。 You can only delay animations. 你只能延迟动画。 You could use setTimeout or show the element initially hidden and then fade it in: 您可以使用setTimeout或显示最初隐藏的元素,然后将其淡入:

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

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

 $("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> 

This will work 这会奏效

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

    setTimeout(
        function() {
            alert("Called after delay.");
        },
        5000);
});
$yourThing.click(function(){
    setTimeout(function(){do your thing here},5000)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM