简体   繁体   English

PhoneGap中的JQuery递归函数

[英]JQuery recursive function in PhoneGap

I have been experimenting with PhoneGap and managed to create a basic memory game, which fades a sequence of numbers in and out, expecting the user to repeat it correctly. 我一直在尝试使用PhoneGap,并设法创建了一个基本的记忆游戏,该游戏会淡入和淡出一系列数字,并期望用户正确地重复该过程。 There's only the bare bones so you wont get any confirmation of a click until you finish the sequence. 只有裸露的骨头,因此在完成序列之前,您不会获得任何点击确认。

The game can be accessed here: http://jdtremelling.com/mobile/index.html 您可以在以下位置访问该游戏: http : //jdtremelling.com/mobile/index.html

And if you fancy it, for testing purposes, the app can be downloaded by scanning this QR code: 如果您愿意,可以出于测试目的,通过扫描以下QR码来下载该应用: 在此处输入图片说明

The problem is that only one number flashes in the sequence when the app loads and then nothing else happens. 问题在于,当应用加载时,序列中只有一个数字闪烁,然后什么也没有发生。 Its as if the recursive function never executes. 好像递归函数从不执行。

The code to initialise the JS array with lists of random numbers (increasing the sizes for each level) , then show the first sequence: 用随机数列表初始化JS数组的代码(增加每个级别的大小),然后显示第一个序列:

function initialiseArrays(){
                for(var i=4; i<9; i++){
                    var numbers = [];
                    for(var j=0; j<i; j++){
                        numbers.push(Math.floor(Math.random() * (max - min) + min));
                    }
                    allNumbers.push(numbers.slice());
                }

                fadeThemOut(allNumbers[0]);
            }

The recursive JavaScript funciton: 递归JavaScript函数:

    function fadeThemOut(children) {
var tmp = [];
   for(var i=0; i<children.length; i++){
      tmp[i] = children[i];
   }
   if (tmp.length > 0) {
      var currentChild = tmp.shift();
      switch(currentChild){
         case 1:
            $('#one').fadeOut('slow').delay( 800 ).fadeIn('slow', function() {
               fadeThemOut(tmp);
            });
            break;
         case 2:
             $('#two').fadeOut('slow').delay( 800 ).fadeIn('slow', function() {
                fadeThemOut(tmp);
             });
             break;
         case 3:
             $('#three').fadeOut('slow').delay( 800 ).fadeIn('slow', function() {
             fadeThemOut(tmp);
         });
         break;
       }
   }
}

Try changing: 尝试更改:

fadeThemOut(allNumbers[0]);

to

fadeThemOut(allNumbers);

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

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