javascript 设置文档准备超时,如何设置

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

Setting a document ready timeout, how to set it up

javascriptjquery

提问by AlexB

Hopefully it will be a quick and easy question,

希望这将是一个快速而简单的问题,

I am creating a simple function, but need to trigger it about 3-4 seconds after the page is loaded, just do not know how.

我正在创建一个简单的函数,但是需要在页面加载后大约 3-4 秒触发它,只是不知道如何。

Here is my script

这是我的脚本

$(function () {
    var slideout = $('#slideout');
    slideout.animate({
        right: '-200px'
    }, 1000, function () {});
    $(".out").toggle(function () {
        $(this).addClass('in');
        slideout.animate({
            right: '0px'
        }, {
            queue: false,
             duration: 500
        });
    }, function () {
        $(this).removeClass('in');
        slideout.animate({
            right: '-200px'
        }, {
            queue: false,
            duration: 500
        });
    });
    $(".close").click(function () {
        $(this).removeClass('out');
        slideout.animate({
            right: '-200px'
        }, {
            queue: false,
            duration: 1000
        });
        slideout.fadeOut({
            duration: 1000
        });
    });
});

Any help is highly appreciated.

任何帮助都受到高度赞赏。

回答by Vicky Gonsalves

$(document).ready(function(){
   setTimeout(function(){

         //YOUR CODE

   },4000);
});

回答by Ravi Hamsa

$(function () {
    var doInteresting = function () {
        var slideout = $('#slideout');
        slideout.animate({
            right: '-200px'
        }, 1000, function () {});
        $(".out").toggle(function () {
            $(this).addClass('in');
            slideout.animate({
                right: '0px'
            }, {
                queue: false,
                duration: 500
            });
        }, function () {
            $(this).removeClass('in');
            slideout.animate({
                right: '-200px'
            }, {
                queue: false,
                duration: 500
            });
        });
        $(".close").click(function () {
            $(this).removeClass('out');
            slideout.animate({
                right: '-200px'
            }, {
                queue: false,
                duration: 1000
            });
            slideout.fadeOut({
                duration: 1000
            });
        });
    }

    setTimeout(doInteresting, 3000);
});