I have a list of numbers in an array. One by one with each number in the array, a request is made to an external website (with the number being a url variable) and the contents of the webpage is displayed.
数组中有一个数字列表。数组中的每个数字一个接一个地向外部网站发出请求(这个数字是一个url变量),并显示网页的内容。
Rather than listing all the numbers (1, 2, 3, 4, 5, 6, 7, 8 ,9) in the array, how can I add 1 every time a request is made to test2.php until the number is 9 (as this is the last in the array)?
而不是在数组中列出所有的数字(1、2、3、4、5、6、7、8、9),如何在每次请求test2时添加1。直到数字是9(因为这是数组中的最后一个)?
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$({}).queue("ajax", $.map(arr, function(item, i) {
return function(next) {
return $.ajax({
type: "POST",
url: "test2.php",
data: {n:item}
})
.then(function(data) {
$("#here").append(data);
next();
});
}
})).dequeue("ajax")
test2.php
test2.php
if (isset($_POST["n"])) {
$content = file_get_contents("https://www.example.com?id=" . $_POST["n"]);
echo $content
}
4 个解决方案
#1
1
Here's the code:
这是代码:
$({}).queue("ajax", $.map(new Array(9), function(item, i) { // ← notice here
return function(next) {
return $.ajax({
type: "POST",
url: "test2.php",
data: {n: i + 1 } // ← and here
})
.then(function(data) {
$("#here").append(data);
next();
});
}
})).dequeue("ajax");
What changed is that I used the index (i) + 1
reference instead of the item. This works because i
starts from 0, and will increment by 1 until it reacher array.length - 1
, and in this case I didn't need a for loop and passed a an array of 9 undefined elements (new Array(9)
) as parameters in $.map
改变的是,我使用索引(I) + 1引用而不是项目。因为i从0开始,然后增加1,直到它进入数组。长度- 1,在本例中,我不需要for循环,而是在$.map中传递了一个包含9个未定义元素(新数组(9))的数组作为参数
The code is very unsemantically correct, but it works for your purposes without changing too much of your code
代码在语义上是非常不正确的,但是它适用于您的目的,并且不会改变太多的代码
#2
1
Why not just loop?
为什么不循环?
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for(var i = 0; i < arr.length; i++ ) {
$.ajax({
type: "POST",
url: "test2.php",
data: {n:arr[i]}
})
.then(function(data) {
$("#here").append(data);
});
}
Of course, the display order is left to chance like this, whereas your original function maintains the array order. If that's a requirement, then you'd want to create an array of functions in the loop and then pass it to the $.queue() function, like so:
当然,显示顺序是这样的,而您的原始函数维护数组顺序。如果这是一个需求,那么您希望在循环中创建一个函数数组,然后将其传递给$.queue()函数,如下所示:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var q = [];
for(var i = 0; i < arr.length; i++ ) {
q.push((function(i){
return function(next){
$.ajax({
type: "POST",
url: "test2.php",
data: {n:arr[i]}
})
.then(function(data) {
$("#here").append(data);
next();
});
}
})(i));
}
$({}).queue("ajax", q).dequeue("ajax");
Removing the array and just using the loop index:
删除数组,只使用循环索引:
var q = [];
var q_start = 1;
var q_end = 9;
for(var i = q_start; i <= q_end; i++ ) {
q.push((function(i){
return function(next){
$.ajax({
type: "POST",
url: "_test2.php",
data: {n:i}
})
.then(function(data) {
$("#here").append(data);
next();
});
}
})(i));
}
$({}).queue("ajax", q).dequeue("ajax");
#3
0
There are other ways to do this but following your scenario, your PHP file could look like:
还有其他方法可以做到这一点,但是根据您的场景,您的PHP文件可以如下:
if (isset($_POST["n"])) {
$pages = array();
$number = intval($_POST['n']);
if ($number > 0 ) {
for( $i = 1; $i <= $number; $i++ ) {
$pages[] = file_get_contents("https://www.example.com?id=" . $i);
}
}
return $pages;
}
Then in the AJAX success function you take care of displaying the collected content.
然后在AJAX success函数中,您将负责显示所收集的内容。
#4
0
Please try this
请试试这个
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script>
var i = 1;
jQuery(document).ready(function(){
function callAjax() {
$.ajax({
type: "POST",
url: "test2.php",
data: {n:i},
success:function(data){
$("#here").append(data);
if ( i++ < 9 ) {
callAjax();
}
}
});
}
callAjax();
});
Please Make sure your have #here div in html
请确保在html中有#here div
#1
1
Here's the code:
这是代码:
$({}).queue("ajax", $.map(new Array(9), function(item, i) { // ← notice here
return function(next) {
return $.ajax({
type: "POST",
url: "test2.php",
data: {n: i + 1 } // ← and here
})
.then(function(data) {
$("#here").append(data);
next();
});
}
})).dequeue("ajax");
What changed is that I used the index (i) + 1
reference instead of the item. This works because i
starts from 0, and will increment by 1 until it reacher array.length - 1
, and in this case I didn't need a for loop and passed a an array of 9 undefined elements (new Array(9)
) as parameters in $.map
改变的是,我使用索引(I) + 1引用而不是项目。因为i从0开始,然后增加1,直到它进入数组。长度- 1,在本例中,我不需要for循环,而是在$.map中传递了一个包含9个未定义元素(新数组(9))的数组作为参数
The code is very unsemantically correct, but it works for your purposes without changing too much of your code
代码在语义上是非常不正确的,但是它适用于您的目的,并且不会改变太多的代码
#2
1
Why not just loop?
为什么不循环?
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for(var i = 0; i < arr.length; i++ ) {
$.ajax({
type: "POST",
url: "test2.php",
data: {n:arr[i]}
})
.then(function(data) {
$("#here").append(data);
});
}
Of course, the display order is left to chance like this, whereas your original function maintains the array order. If that's a requirement, then you'd want to create an array of functions in the loop and then pass it to the $.queue() function, like so:
当然,显示顺序是这样的,而您的原始函数维护数组顺序。如果这是一个需求,那么您希望在循环中创建一个函数数组,然后将其传递给$.queue()函数,如下所示:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var q = [];
for(var i = 0; i < arr.length; i++ ) {
q.push((function(i){
return function(next){
$.ajax({
type: "POST",
url: "test2.php",
data: {n:arr[i]}
})
.then(function(data) {
$("#here").append(data);
next();
});
}
})(i));
}
$({}).queue("ajax", q).dequeue("ajax");
Removing the array and just using the loop index:
删除数组,只使用循环索引:
var q = [];
var q_start = 1;
var q_end = 9;
for(var i = q_start; i <= q_end; i++ ) {
q.push((function(i){
return function(next){
$.ajax({
type: "POST",
url: "_test2.php",
data: {n:i}
})
.then(function(data) {
$("#here").append(data);
next();
});
}
})(i));
}
$({}).queue("ajax", q).dequeue("ajax");
#3
0
There are other ways to do this but following your scenario, your PHP file could look like:
还有其他方法可以做到这一点,但是根据您的场景,您的PHP文件可以如下:
if (isset($_POST["n"])) {
$pages = array();
$number = intval($_POST['n']);
if ($number > 0 ) {
for( $i = 1; $i <= $number; $i++ ) {
$pages[] = file_get_contents("https://www.example.com?id=" . $i);
}
}
return $pages;
}
Then in the AJAX success function you take care of displaying the collected content.
然后在AJAX success函数中,您将负责显示所收集的内容。
#4
0
Please try this
请试试这个
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script>
var i = 1;
jQuery(document).ready(function(){
function callAjax() {
$.ajax({
type: "POST",
url: "test2.php",
data: {n:i},
success:function(data){
$("#here").append(data);
if ( i++ < 9 ) {
callAjax();
}
}
});
}
callAjax();
});
Please Make sure your have #here div in html
请确保在html中有#here div