循环遍历页面以获取URL并使用fetch更改URL

时间:2017-11-02 20:37:35

标签: javascript fetch-api

我希望它能够在页面中移动网址并获取下一个网址,依此类推,直到绘图完成为止

function linkedDrawing(canvas,url){
    fetch(url)
   .then((response)=> response.json())
   .then((data)=>{
       url = data.url;
       alert(data.url);
       const c = canvas.getContext('2d');
       c.beginPath();
       c.moveTo(data.x1, data.y1);
       c.lineTo(data.x2, data.y2);
       c.strokeStyle = data.col;
       c.stroke();
     })
   }
 }
 linkedDrawing(linkedDrawingCanvas, "http://jacek.soc.port.ac.uk/tmp/ws/alpha.json")

我的小提琴:http://jsfiddle.net/g1nqkn7h/3/

2 个答案:

答案 0 :(得分:0)

我已经用这种方式编辑了你的代码:

public async Task<IActionResult> Delete([FromBody] Dto data)

答案 1 :(得分:0)

return来自该函数的fetch()调用并安排下一次调用相同的函数,另请参阅multiple, sequential fetch() Promise

const canvas = document.getElementById("linkedDrawingCanvas");
const c = canvas.getContext('2d');

function draw(response) { 
  return response.json()
  .then(data => {       
    console.log(data.url);
    c.beginPath();
    c.moveTo(data.x1, data.y1);
    c.lineTo(data.x2, data.y2);
    c.strokeStyle = data.col;
    c.stroke();
    if (data.url) {
      return linkedDrawing(linkedDrawingCanvas, data.url).then(draw)
    } else {
      return "complete"
    }
  })
}

function linkedDrawing(canvas,url){
  return fetch(url)
  .then(draw)
}

linkedDrawing(linkedDrawingCanvas, "http://jacek.soc.port.ac.uk/tmp/ws/alpha.json")