Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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 使我的MetroJs初始化更高效、更干净_Javascript_Jquery_Jquery Plugins_Metro.js - Fatal编程技术网

Javascript 使我的MetroJs初始化更高效、更干净

Javascript 使我的MetroJs初始化更高效、更干净,javascript,jquery,jquery-plugins,metro.js,Javascript,Jquery,Jquery Plugins,Metro.js,我使用了一个单独的函数来初始化每个磁贴,因为我一次只想看到一个磁贴的背面。因此,使用此代码,每个平铺都会完全设置动画,显示背面和正面,然后暂停一段时间,以便其他平铺也执行相同的操作 不管怎么说,这并不重要,因为你可以看到我的代码现在效率很低,有很多重复代码。你认为我该如何改进它,使它短得多?我曾经考虑过使用一个循环来初始化它们,但我不知道该怎么做 我使用单个初始化器的原因是我需要创建一个适用于每个磁贴的计数,但我无法从.liveTile()中创建计数变量 JS: 使count变量更干净的一种方法

我使用了一个单独的函数来初始化每个磁贴,因为我一次只想看到一个磁贴的背面。因此,使用此代码,每个平铺都会完全设置动画,显示背面和正面,然后暂停一段时间,以便其他平铺也执行相同的操作

不管怎么说,这并不重要,因为你可以看到我的代码现在效率很低,有很多重复代码。你认为我该如何改进它,使它短得多?我曾经考虑过使用一个循环来初始化它们,但我不知道该怎么做

我使用单个初始化器的原因是我需要创建一个适用于每个磁贴的计数,但我无法从
.liveTile()中创建计数变量

JS:


使
count
变量更干净的一种方法是将每次初始化都移动到一个函数中,这会在该函数中创建count变量,但同样地,我需要使其余变量更高效。

能否将
数据count='0'
添加到
.live tile
项目中

$('.live-tile').liveTile({
    animationComplete:function() {
        var count = parseInt($(this).data('count'));
        count++;
        $(this).data('count', count);
        if (count == 2) {
            $(this).liveTile("restart", 10000);
            $(this).data('count', '0');
        }
    }
});
如果没有,也可以执行以下操作:

var counts =[0, 0, 0, 0, 0;]

$('.live-tile').liveTile({//.find will let you access all divs (.one, .two, etc.) at once that .live-tile contains
    animationComplete:function() {
        var index = $(this).index();//the index of div item (among the div items of .live-tile, it will be 0 for .one, 1 for .two, 2 for .three, etc.
        var count = counts[index];//bring the current value based on the index
        count++;//increase it 
        counts[index] = count;//restore it to the correct index
        if (count == 2) {
            $(this).liveTile("restart", 10000);
            counts[index] = 0);//reset
        }
    }
});

我不这么认为,我加入了计数,这样我就可以手动跟踪我在瓷砖背面/正面迭代了多少次。我只想在短时间内展示一下。太好了,第二个代码工作得很好。你能解释一下
查找
索引
是如何工作的吗?那会很有帮助的。@germainelol你能查一下我的解释吗?如果有帮助,请不要忘记将其标记为正确答案。它实际上可以在没有
的情况下工作。查找
,但不能使用它,因为它不会初始化
liveTile的
。谢谢。它如何决定
索引中分幅的顺序?它是根据HTML中的编写顺序创建的吗?因此,它接收找到的第一个
live tile
,并将其添加到索引中,依此类推。
var counts =[0, 0, 0, 0, 0;]

$('.live-tile').liveTile({//.find will let you access all divs (.one, .two, etc.) at once that .live-tile contains
    animationComplete:function() {
        var index = $(this).index();//the index of div item (among the div items of .live-tile, it will be 0 for .one, 1 for .two, 2 for .three, etc.
        var count = counts[index];//bring the current value based on the index
        count++;//increase it 
        counts[index] = count;//restore it to the correct index
        if (count == 2) {
            $(this).liveTile("restart", 10000);
            counts[index] = 0);//reset
        }
    }
});