- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
更新如下
我正在尝试做的是以 block 为单位遍历数组,从一个 block 到另一个 block 交替迭代的方向。使困惑?我也是。例如,如果我想遍历一个包含 25 个元素的数组,但我想按以下顺序进行:0、1、2、3、4、9、8、7、6、5、10 , 11, 12, 13, 14, 19, 18, 17, 16, 15, 20, 21, 22, 23, 24,最有效的方法是什么?我正在寻找可扩展的东西,因为我现在使用的数组实际上是 225 个元素,我想以 15 个元素 block 的形式遍历它,但这在某些时候可能会改变。到目前为止,我发现唯一可行的方法是将迭代顺序硬连接到第二个数组,然后以正常方式迭代它以获取原始数组的索引。但这很糟糕。任何帮助将不胜感激。
@Bergi 要求提供一些示例代码。请不要打我太多。我还是菜鸟:
function zeroPadNumber(theNumber, thePadding) {
var thePaddedNumber = thePadding.substring(0, (thePadding.length - theNumber.length)) + theNumber;
return thePaddedNumber;
}
var thisTile = 225;
var waveLoop = 15;
function mosaicWave() {
var theStartNum = thisTile;
for (w = 0; w < 15; w++) {
var theNum = theStartNum - w;
var theNumString = String(theNum);
var thePaddedNum = zeroPadNumber(theNumString, "000");
var theImgName = "sm_" + thePaddedNum;
var theNewSrc = theImgFolder + theImgName + "bg.gif";
document.images[theImgName].src = theNewSrc;
thisTile = theNum - 1;
if (waveLoop < 15) {
var prevStartTile = theStartNum + 15;
var thePrevNum = prevStartTile - w;
var thePrevNumString = String(thePrevNum);
var thePrevPaddedNum = zeroPadNumber(thePrevNumString, "000");
var thePrevName = "sm_" + thePrevPaddedNum;
var thePrevSrc = theImgFolder + thePrevName + ".gif";
document.images[thePrevName].src = thePrevSrc;
}
}
if (waveLoop == 1) {
var lastWave = function() {
var theStartNum = 15;
for (c = 0; c < 15; c++) {
var theNum = theStartNum - c;
var theNumString = String(theNum);
var thePaddedNum = zeroPadNumber(theNumString, "000");
var theImgName = "sm_" + thePaddedNum;
var theNewSrc = theImgFolder + theImgName + ".gif";
document.images[theImgName].src = theNewSrc;
}
}
setTimeout(lastWave, 100);
waveLoop = 15;
thisTile = 225;
} else {
waveLoop--;
setTimeout(mosaicWave, 100);
}
}
这个片段做了一个不同的动画。它从矩阵的右下角开始,“打开”底行的 15 个方 block 。然后它向上移动一行,打开该行中的瓷砖并关闭前一行中的瓷砖。依此类推,直到顶行打开然后关闭。与我试图在新函数中实现的自上而下的蛇形效果相差不远。每行的颠倒顺序是难倒我的主要问题。也就是说,任何有关优化上述代码的建议也将不胜感激。
更新 1:
对我来说,这似乎应该行得通,但实际上行不通。任何人都可以发现问题吗?
var loopRange = 225;
var blockRange = 15;
var theDirection = 1;
var weaveLoop = 0;
function mosaicWeave() {
var curObj, curSrc, lastObj, lastSrc;
var toggleLeadTile = function() {
alert(curSrc);
curObj.src = curSrc;
};
var toggleLastTile = function() {
lastObj.src = lastSrc;
};
while (weaveLoop < loopRange) {
imgNum = weaveLoop + 1;
imgName = "sm_" + zeroPadNumber(String(imgNum), "000");
if (imgNum < 15) {
//handle first row
curObj = document.images[imgName];
curSrc = theImgFolder + imgName + "bg.gif";
window.setTimeout(toggleLeadTile, 100);
} else if (imgNum == 225) {
//handle last row
curObj = document.images[imgName].src;
curSrc = theImgFolder + imgName + "bg.gif";
window.setTimeout(toggleLeadTile, 100);
for (i = 211; i < 226; i++) {
lastImgName = "sm_" + ((weaveLoop + 1) - 15);
lastObj = document.images[lastImgName];
lastSrc = theImgFolder + lastImgName + ".gif";
window.setTimeout(toggleLastTile, 100);
}
} else {
//handle middle rows
lastImgName = "sm_" + ((weaveLoop + 1) - 15);
curObj = document.images[imgName];
curSrc = theImgFolder + imgName + "bg.gif";
lastObj = document.images[lastImgName];
lastSrc = theImgFolder + lastImgName + ".gif";
window.setTimeout(toggleLeadTile, 100);
window.setTimeout(toggleLastTile, 100);
}
if (weaveLoop % blockRange == (theDirection == -1 ? 0 : blockRange - 1)) {
theDirection *= -1;
weaveLoop += blockRange;
} else {
weaveLoop += theDirection;
}
}
}
更新 2:
感谢大家的意见。这有效:
var resetLoop = 1;
var weaveArray = new Array(225);
var weaveRange = 15, weaveDirection = 1, weaveIndex = 0, wInitLoop = 0;
function mosaicWeave() {
while (weaveIndex < 225) {
weaveArray[wInitLoop] = weaveIndex + 1;
if (weaveIndex % weaveRange == (weaveDirection == -1 ? 0 : weaveRange - 1)) {
weaveDirection *= -1;
weaveIndex += weaveRange;
} else {
weaveIndex += weaveDirection;
}
wInitLoop++;
}
mWeaveOn();
}
function mWeaveOff() {
var theNumString = String(weaveArray[resetLoop - 16]);
var theImgName = "sm_" + zeroPadNumber(theNumString, "000");
document.images[theImgName].src = "images/" + theImgName + ".gif";
mosaicArray[resetLoop - 1] = 0;
resetLoop++;
if (resetLoop < 226) {
setTimeout(mWeaveOn, 25);
} else if (resetLoop > 225 && resetLoop <= 240) {
setTimeout(mWeaveOff, 25);
} else {
resetLoop = 1;
}
}
function mWeaveOn() {
var theNumString = String(weaveArray[resetLoop - 1]);
var theImgName = "sm_" + zeroPadNumber(theNumString, "000");
document.images[theImgName].src = "images/" + theImgName + "bg.gif";
mosaicArray[resetLoop - 1] = 1;
if (resetLoop < 16) {
resetLoop++;
setTimeout(mWeaveOn, 25);
} else {
setTimeout(mWeaveOff, 25);
}
}
有人对是否有更有效的方法有意见吗?或者提前了解这在不同平台/浏览器或不同情况下可能会如何崩溃?再次感谢。
最佳答案
var arr = [0,1,2,3,4,5,6,7,8,9,10,11,11,13,14,15,16,17,18,19,20,21,22,23,24],
i = 0,
j = arr.length,
tmp,
chunk = 5;
while(i < j) {
tmp = arr.slice(i, i+=chunk);
if ((i / chunk) % 2 == 0) {
tmp = tmp.reverse();
}
console.log(tmp);
}
关于javascript - 前后交替执行javascsript for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12880669/
我想交织相同模式和相等长度的两个向量。说: a <- rpois(lambda=3,n=5e5) b <- rpois(lambda=4,n=5e5) 我想交织或交织这两个向量,以创建一个等效于c(a
我有两个矩阵,我想以交替方式交织/交错/堆叠在彼此之上/rbind。 ranks=1:3 names=c("Karl", "Klaus", "Mary") x <- cbind(ranks, name
我在 JavaScript 中有一个字符串,如下所示: "This {{@is}} a $|test$| string with $|@string$| delimiters {{as}} follo
那么,我正在尝试“合并”一个字符串 (a) 和一个字符串列表 (b): a = '1234' b = ['+', '-', ''] 获得所需的输出(c): c = '1+2-34' 所需输出字符串中的
我有几个可变长度的数组,其中填充了表示好数据 block 和坏数据 block 的元组。 input = [ [(True, 0, 400), (False, 400, 500), (True
我有一个包含 3 个单元格的表,我想知道是否可以在每个单元格之间“旋转”数据? 基本上,在设定的时间后,我希望第一个单元格中的数据移动到第二个单元格,第二个单元格数据移动到第三个单元格,第三个单元格数
使用 RichFaces dataTable 组件交替行颜色的最简单方法是什么? 最佳答案 查找组件的 rowClasses 属性。 抱歉.. 阅读 richfaces 文档两秒钟后就明白了。 :)
我想创建一个有 4 列的表格,但只显示 3 列。所以我理想情况下有一个跨越 3 列的 div 并应用溢出:隐藏。单击按钮时,我希望第 3 列(公司 2)向左滑动,并替换为第 4 列(公司 3),以便将
有没有办法在 AngularJS 中替代 ng-repeats ? 例如,假设我有两个数组。 数组1 [a1, a2, a3, ...] 数组2 [b1, b2, b3, ...] 我想针对中型和大屏
我有三个包含元素的列表: a = [[0,1],[2,3],...] b = [[5,6],[7,8],...] c = [] 我想将 a 和 b 中的元素 append 到 c 中以获得: c =
我喜欢在 MATLAB 中绘制经过傅立叶变换的信号。通过 set(gca,'xtick',peaks,'FontSize',12);我可以在 x 轴上显示峰值。但有时,峰值靠得太近,显示峰值的文本与其
我正在使用 CSS Grid 为我的网站构建服务列表。整个网站网格中的一行被分成两个 CSS 网格列。 在第一行的第一列中,有一项服务的描述。在第二列中,有一个代表服务的图像。 每一行,描述和图像交替
我有以下 html 代码: 1 2 3 4 5 6 7 8 我想做的是使用奇数/偶数 nth-child 选择器对它们进行一些交替
在下面的代码示例中,我将 window.status 从“a”替换为“b” function alternateViaIntrvl() { setInterval('alterStatus()
下面的CSS和HTML代码生成 News Interviews ---------------------- Djing Break dance ---------------------- 为什么横
我曾经在 tableView willDisplay cell 方法中使用这段代码,但它没有准确地交替颜色 - 它几乎做到了,但有时仍然搞砸了 1 或 2 个相同的颜色和我不确定。 我发现一些建议在我
我的问题的本质是解决方案太多,我想在围绕它构建基础设施之前找出哪一个在优缺点中胜出。 (为了本论坛的目的进行了简化)这是一个拍卖网站,其中五个拍卖按排名#1-5 存储,#1 是当前特色拍卖。其他四个人
如果可能的话,我正在尝试找出是否有一种方法可以替换内容行分隔符的颜色。 例如: 问题是它必须是自动的,所以我假设可能需要 javascript,但我找不到这样的东西。我知道有些事情表明如果你有类似 t
这个看似简单的问题困扰了我整整 10 年。 (好的,不是每天!) 它在 IE 中运行良好,但在 FF 和 Chrome 中运行不正常,这通常表示代码有问题...我想在两侧都有 DIV 框(它们实际上是
我想找到交替 [0, 1, 0, 1] 所需的最少翻转次数,例如给定 [1, 1, 0, 1]。所以在这种情况下,它是一次翻转。 def solution(A): count = 0
我是一名优秀的程序员,十分优秀!