关于广泛使用的requestAnimationFrame()函数,我有一些问题.最近我在多人游戏中遇到了一些在客户端而不是服务器端使用它的实现.
我在动画和游戏循环之间有点混淆 - 我正在寻找的是NodeJS中的实现=>例如setInterval.
(function () {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x] + 'CancelAnimationFrame'] ||
window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = …Run Code Online (Sandbox Code Playgroud) 免责声明:这个问题并不像同此一个.
我有一个嵌套数组的例子:
var testArray = [
true,
"",
[
1,
{a: 2, b: [3, false]},
[2, []],
null
],
4,
undefined,
[5, "test"],
function() {}
];
Run Code Online (Sandbox Code Playgroud)
如何在嵌套数组中获取valueOf,如:
testArray.multiIndexOf(null); //Expected result will be [2, 3]
Run Code Online (Sandbox Code Playgroud)
我将解释这里发生的事情.
首先,我们将testArray分解为:
var testArrayExplain = [0, 1, [0, 1, [0, 1], 3], 3, 4, [0, 1], 6];
Run Code Online (Sandbox Code Playgroud)
正如你在这里看到的,这个数组和上面的数组一样:第一层数组的长度为7,然后看一下位置#2的值,我们将看到另一个数组嵌套到第一层.正如您从上面记得的那样,该null值与第一个数组的位置#2处的数组中的位置#3具有相同的确切位置
因此,我们将具有第一层位置,然后是第二层位置null:,[2, 3]存储在从第一层到最后一层排序的数组中.
另一个例子:
var testArrayTwo = [1,[2,3,4],5,[6,[7,8],9,10],11,12];
testArrayTwo.multiIndexOf(6); //return [3, 0]
Run Code Online (Sandbox Code Playgroud)
可选:如果嵌套数组有两个相同的值,那么获取第一个值的位置,还是存储两个位置的数组?如果value不在嵌套数组中,return …
我有一个嵌套数组的例子:
var testArray = [1,2,[3,4,[5,6],7],8,9,[10,11],12];
Run Code Online (Sandbox Code Playgroud)
这是我获取嵌套数组长度的函数:
Array.prototype.getLength = function() {
var sum = 0;
function getMultiLength(array) {
for (count = 0; count < array.length; count ++) {
sum ++;
if (!array[count].length) {
getMultiLength(array[count]);
}
}
}
getMultiLength(this.valueOf());
return sum;
};
Run Code Online (Sandbox Code Playgroud)
我对结果的期望是12,但我得到的是无限循环:
testArray.getLength(); //infinite loop
Run Code Online (Sandbox Code Playgroud)
任何人都知道为什么以及如何获得嵌套数组长度?
javascript arrays function infinite-loop multidimensional-array
我有一个函数来描述我的问题:
function testingFunc(value) {
var temp = value;
console.log("temp before: " + JSON.stringify(temp));
console.log("arrayTest before: " + JSON.stringify(arrayTest));
temp.unshift(123);
console.log("temp after unshift: " + JSON.stringify(temp));
console.log("arrayTest after unshift: " + JSON.stringify(arrayTest));
temp.push(456);
console.log("temp after push: " + JSON.stringify(temp));
console.log("arrayTest after push: " + JSON.stringify(arrayTest));
temp.shift();
console.log("temp after shift: " + JSON.stringify(temp));
console.log("arrayTest after shift: " + JSON.stringify(arrayTest));
temp.pop();
console.log("temp after pop: " + JSON.stringify(temp));
console.log("arrayTest after pop: " + JSON.stringify(arrayTest));
return temp;
}
var arrayTest = [1,2,3,4,5];
var arrayTestTwo;
arrayTestTwo …Run Code Online (Sandbox Code Playgroud)