如何多次调用getJson并在它完成第一个传递项后传递值

How to call getJson multiple times and pass it values after it finishes the first passed item?

本文关键字:第一个 调用 何多次 getJson      更新时间:2023-09-26

我有一个getjson调用,我想传递一些项目给它。我想知道我如何多次调用这个getjson并传递它们的值,如book, pen, paper,…如果这些商品打折,显示在不同的框架中,如下所示(例如,我想检查10个商品是否打折)?

目前,我的代码只处理一个项目,但我希望它处理10个项目,并显示那些正在出售的项目。

此外,如果用户在页面中,我希望每隔10分钟对商品列表执行此销售检查过程,而无需用户重新加载页面。你们能告诉我怎么做吗?

<script src="http://anyorigin.com/jquery-1.4.2.min.js"></script>
<script>
$.getJSON('http://anyorigin.com/get?url=http://www.awebsite.com/item=book/&callback=?', function(data){
  var siteContents = data.contents;
  //writes to textarea
  //document.myform.outputtext.value = siteContents;
  var n=siteContents.search("This item is not on sale");
  alert("value of n:"+n);
  if(n=-1) {
    alert("item book is on sale. n:"+n);
    $('#ItemBookWrapper').show();
    //if the passed item is on sale display that item in a iframe if its not on sale go   //to next item. so at
  } else {
    alert("item:book is not on sale");
  }
});
</script>
</head>
<body>
  <div id="ItemBookWrapper" style="display:none">
    <iframe src='http://www.awebsite.com/item=book' height=200 width=200 style='border: none;'></iframe>
  </div>
  <br>

我想知道我如何调用这个getjson多次并传递他们的值,如书,笔,纸

如果这是网站使用的格式…

http://www.awebsite.com/item=book/&callback=?

你从哪里得到书,笔,纸…你只需要创建一个字符串

var getJSONurl = "http://anyorigin.com/get?url=http://www.awebsite.com/item=" + itemType +/&callback=?"

然后

$.getJSON(getJSONurl, function(data) {
   // your code
}

目前,我的代码只处理一个项目,但我希望它处理10个项目,并显示那些正在出售的项目

查看JSON结构会对这个有很大帮助,但是,通常使用JSON时,你会使用…

obj = JSON.parse(data);

,然后使用迭代器循环遍历所有的数据对象。

如果用户在页面中,我想每10分钟对商品列表进行一次销售检查过程,而无需用户重新加载页面

使用(setInterval)[https://developer.mozilla.org/en-US/docs/DOM/window.setInterval]函数…

使用

的地方
var interval = window.setInterval(yourJSONfunction(), 60000)

其中60000是以毫秒为单位的时间(600秒= 10分钟)。当页面加载时,它将每10分钟调用一次。你可以把一个变量设置成你想要的任何东西,不管它是书,纸,还是别的什么。并使用它来完成字符串

相关文章: