简体   繁体   English

setTimeout()或delay()在js函数中不起作用

[英]setTimeout() or delay() not working in js function

I want to add delay or setTimeout after the startSound() but could not able to add. 我想在startSound()之后添加延迟或setTimeout,但无法添加。 If I add, it not working. 如果我添加,它将无法正常工作。 I want a delay of 5-10sec after the sound is complete. 我希望声音完成后有5到10秒的延迟。 And also want to add an html code like "YOU WIN $2000" after startSound() . 并且还想在startSound()之后添加一个类似“ YOU WIN $ 2000”的html代码。

JS CODE: JS代码:

self.rightAnswer = function(elm) {

    $("#" + elm).fadeOut(1500, function() {
        if (self.level() == 5 || self.level() == 10) {

        startSound('wrongsound2', false);

        $("#" + elm).css('background', 'url(boxright.png) no-repeat 0 0').fadeIn(800, function() {
                self.money($(".active").data('amt'));
                if(self.level() + 1 > 15) {
                    $("#game").fadeOut('slow', function() {
                        $("#game-over").html('You Win!');
                        $("#game-over").fadeIn('slow');
                    });
                } else {
                    self.level(self.level() + 1);
                    $("#" + elm).css('background','url(box.png) no-repeat 0 0');
                    $("#" + elm).hover(function(){
                        $(this).css("background", "url(boxhover.png) no-repeat 0 0");
                    }, function(){
                        $(this).css('background','url(box.png) no-repeat 0 0');
                        });
                    $("#answer-one").show();
                    $("#answer-two").show();
                    $("#answer-three").show();
                    $("#answer-four").show();
                    self.transitioning = false;
                }
            });
            }

Well, you did not write how you tried to use that function. 嗯,你没写你如何试图使用该功能。 The stardard is something like that: 标准品是这样的:

self.rightAnswer = function(elm) {

    $("#" + elm).fadeOut(1500, function() {
        if (self.level() == 5 || self.level() == 10) {

            startSound('wrongsound2', false);
            setTimeout(function(){
                // whatever commands you want to get executed after 5 seconds go here...
            }, 5000);
            [...]

So basically you hand over some function to the setTimeout() call (an anonymous function here) and the desired time to delay in milli seconds. 因此,基本上,您setTimeout()一些函数移交给setTimeout()调用(此处为匿名函数),并延迟所需的时间(以毫秒为单位)。 That's all. 就这样。

Note: javascript (mostly) follows an asynchronous approach. 注意: javascript(大多数情况下)遵循异步方法。 The script execution is not delayed for that amount of time. 脚本执行不会延迟该时间量。 So any commands after the call to the setTimeout() function are executed immediately. 因此,调用setTimeout()函数之后的所有命令都将立即执行。 Only those commands handed over in said function are delayed because that function is called then. 仅延迟在所述功能中移交的那些命令,因为随后将调用该功能。 Think of it like pushing commands into a queue which will get triggered after the specified amount of time. 可以将其视为将命令推入队列,该队列将在指定的时间后触发。 setTimeout() is not a pause/sleep function. setTimeout() 不是暂停/睡眠函数。 It only registers jobs into the delay queue and returns immediately. 它仅将作业注册到延迟队列中并立即返回。

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

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