我需要在脚本上循环一定次数:
脚本:
var x = 3
$.get('{{ shop.domain }}/pageX.html?format=json', function (data) {
//the actual script
});
我需要什么:
我需要脚本来检查这个脚本 3 次:
第一次:
.get('{{ shop.domain }}/page1.html?format=json', function (data) { ...
第二次:
.get('{{ shop.domain }}/page2.html?format=json', function (data) { ...
第三次:
.get('{{ shop.domain }}/page3.html?format=json', function (data) { ...
但是,当 x = 4 时,我需要将脚本检查 4 次...
这可能吗?也许每个..?
谢谢!
最佳答案
这样可以吗?
var x = 3;
for(i = 0; i < x; ++i) {
$.get('{{ shop.domain }}/page' + i + '.html?format=json', function (data) {
//the actual script
});
}
如果您无法多次定义该函数:
function yourFunc(data) {
// ...
}
var x = 3;
for(i = 0; i < x; ++i) {
$.get('{{ shop.domain }}/page' + i + '.html?format=json', yourFunc);
}
关于javascript - 如何循环脚本 x 次。 (使用 x 变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23528654/