Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回变量的值_Javascript_Jquery_Variables_Return_Countdown - Fatal编程技术网

Javascript 返回变量的值

Javascript 返回变量的值,javascript,jquery,variables,return,countdown,Javascript,Jquery,Variables,Return,Countdown,代码: 目标: 当令牌计数达到0时,将其返回到原始值以再次开始另一个计数,因为每当计数达到0时,它将变为-1并继续 当6分钟等待结束时,开始另一次等待,我不知道我是否得到了这部分的正确代码,但请检查 如果您想保留您的代码,但不遵循注释中的建议,请执行以下操作: var n = 360, // 6 min of waiting after blocked count = 50, // Count of tokens when reached 0 block

代码:

目标:

当令牌计数达到0时,将其返回到原始值以再次开始另一个计数,因为每当计数达到0时,它将变为-1并继续

当6分钟等待结束时,开始另一次等待,我不知道我是否得到了这部分的正确代码,但请检查


如果您想保留您的代码,但不遵循注释中的建议,请执行以下操作:

        var n = 360, // 6 min of waiting after blocked
            count = 50, // Count of tokens when reached 0 block the page
            counter = document.getElementById('counter'),            
            clickDisabled = false;

        function countDown(){
            if(n > 0){
                setTimeout(countDown, 1000);
            } else {
                n = 360;
                clickDisabled = false
            }
            $("span#waitcount").html(document.createTextNode(n));
            n--;
        }

        $('.slotMachineButton').click(function () {
            if (clickDisabled) {
                return;
            }
            setTimeout(function () {
                counter.innerHTML = count;
                if (count === 0) {
                    $.blockUI({ message: '<h1>Thank you for Playing!!!<br>Please wait or 6 munites to be able to play again.</h1>' });
                    setTimeout(function () {
                        $.unblockUI({
                            onUnblock: function () {
                                clickDisabled = true;
                                alert('Game has been resumed!!!');                                    
                                setTimeout(countDown, 10);
                            }
                        });
                    }, 1000);
                    count = 50;
                }
                count--;
            });
            clickDisabled = true;
            setTimeout(function () {
                clickDisabled = false;
            }, 100);
        });

巴德,你真的应该找一个不同的方法。我更改了测试的计时器值。

好吧,其中一个缺少括号。另外,如果您想每1000毫秒重复一次函数调用,那么应该使用setInterval,而不是对setTimeout的递归调用。此外,您也没有指定在代码中重置count和n值的位置。最后,如果您唯一的问题是please do check,它不属于StackOverflow。与其减少计数器,不如通过Date保存计时器启动时的时间戳。不时从当前时间中减去该时间,然后查看是否超过360秒。如果不准确,则保证在最短的时间内您的回调将触发;计时器触发回调可能需要更长的时间。计数和n-shout在函数结束时进行,必须重置。
        var n = 360, // 6 min of waiting after blocked
            count = 50, // Count of tokens when reached 0 block the page
            counter = document.getElementById('counter'),            
            clickDisabled = false;

        function countDown(){
            if(n > 0){
                setTimeout(countDown, 1000);
            } else {
                n = 360;
                clickDisabled = false
            }
            $("span#waitcount").html(document.createTextNode(n));
            n--;
        }

        $('.slotMachineButton').click(function () {
            if (clickDisabled) {
                return;
            }
            setTimeout(function () {
                counter.innerHTML = count;
                if (count === 0) {
                    $.blockUI({ message: '<h1>Thank you for Playing!!!<br>Please wait or 6 munites to be able to play again.</h1>' });
                    setTimeout(function () {
                        $.unblockUI({
                            onUnblock: function () {
                                clickDisabled = true;
                                alert('Game has been resumed!!!');                                    
                                setTimeout(countDown, 10);
                            }
                        });
                    }, 1000);
                    count = 50;
                }
                count--;
            });
            clickDisabled = true;
            setTimeout(function () {
                clickDisabled = false;
            }, 100);
        });