javascript - 如何循环脚本 x 次。 (使用 x 变量)

标签 javascript json loops each

我需要在脚本上循环一定次数:

脚本:

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/

相关文章:

java - 如何处理 HTTP 超时?

javascript - Node JS 如何测试上传的文件是否是json文件?

javascript - JavaScript 设置属性

javascript - jQuery : Display hidden image on page scroll

javascript - JS : How to match one capture group multiple times in the same string?

javascript - 如何使用 Express 和 AngularJS 打开带有请求的新链接

json - kubectl 应用错误 : error converting YAML to JSON

javascript - 在数组中存储增量值的快捷方式?

java - 为什么以下两种方法之间存在性能差异?

页面刷新后 JavaScript 幻灯片保持在同一张幻灯片上