简体   繁体   English

在 Javascript 中多次运行命令

[英]Run a command many times in Javascript

I have a scenario in Javascript which runs and it gives me a result in Console.我在 Javascript 中有一个运行场景,它在控制台中给了我一个结果。 In this scenario, I need to insert 5 times a pair of different coordinates (for each time).在这种情况下,我需要插入 5 次一对不同的坐标(每次)。 If I copy-paste the same code 5 times, with different coordinates (which they are inserted in different variables each time), it doesn't run.如果我使用不同的坐标(每次将它们插入到不同的变量中)复制粘贴相同的代码 5 次,它就不会运行。 Below, I put the same code twice with different coordinates as I try the same thing in Jsfiddle.下面,当我在 Jsfiddle 中尝试相同的事情时,我将相同的代码以不同的坐标放置了两次。

//first pair of coordinates:

let x= 38.041242 ,y= 23.679595

let myString = `https://maps.googleapis.com/maps/api/streetview?location=${x},${y}&size=300x300&pitch=90`
getBase64FromImageUrl(myString);

console.log(x,y);


//second pair of coordinates:

let z= 38.041310 ,f= 23.679684

let myString = `https://maps.googleapis.com/maps/api/streetview?location=${z},${f}&size=300x300&pitch=90`
getBase64FromImageUrl(myString);

console.log(z,f);

I suspect as @ThatBrianDude and @Archer have already stated that you are getting an error because you are defining the variable myString more than once.我怀疑 @ThatBrianDude 和 @Archer 已经声明您收到错误,因为您不止一次定义变量 myString。 I would also recommend you create an array of objects instead of copy pasting the code over and over.我还建议您创建一个对象数组,而不是一遍又一遍地复制粘贴代码。

// Array of coordinates to run through.
let coordinates = [{x: 38.041242, y: 23.679595, {x: 38.041310, y: 23.679684}];

// List of images.
let images = coordinates.map((coordinate) => {

  // URL based on coordinates.
  return getBase64FromImageUrl(`https://maps.googleapis.com/maps/api/streetview?location=${coordinate.x},${coordinate.y}&size=300x300&pitch=90`);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM