我在里面调用getjson for循环。目前我的代码打印所有项目并没有省略未售出的物品!
这是检查物品是否在售的部分:
$.getJSON('http://anyorigin.com/get?url=http://www.asite.com/itemtocheck.php='+ itemName + '/&callback=?', function(data){
var siteContents = data.contents;
var n=siteContents.search("This item is not on sale");
if(n=-1)
{
//alert("This item is on sale. n:"+n);
var siteContents2 = "This item is on sale:"+itemName;
document.getElementById("myDiv").innerHTML += siteContents2;
};
我不知道这个if语句有什么问题,即使该商品没有发售,它也总能实现!有没有办法让我在每次调用getjson和#34; for循环"之间有一个延迟(非常短的延迟,所以getjson获取数据并有足够的时间来过滤它)?
我不确定,但我认为可能" for loop"正在快速调用getjson,没有给每个项目足够的时间来检查。我甚至试过这样做:
setTimeout(GetJSONResult('pen'), 500);
但由于某种原因它没有用! 任何人都可以帮助修复我的代码,以便过滤掉未售出的商品吗?谢谢
<script>
function GetJSONResult(itemName)
{
//alert("test:"+itemName);
$.getJSON('http://anyorigin.com/get?url=http://www.asite.com/itemtocheck.php='+ itemName + '/&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");
document.write("value of n"+n);
if(n=-1)
{
//alert("This item is on sale. n:"+n);
var siteContents2 = "This item is on sale:"+itemName;
document.getElementById("myDiv").innerHTML += siteContents2;
};
});
};
items=["pen","paper","book","tshirt","cup","coffee","plate","cherry","apple","mango","orange"];
for (var i=0;i<items.length;i++)
{
document.write(items[i] + "<br>");
GetJSONResult(items[i]);
}
////setTimeout(GetJSONResult('pen'), 500);
////setTimeout(GetJSONResult('paper'), 500);
////setTimeout(GetJSONResult('book'), 500);
////setTimeout(GetJSONResult('tshirt'), 500);
////GetJSONResult('cup');
////GetJSONResult('coffee');
////GetJSONResult('plate');
////GetJSONResult('cherry');
////GetJSONResult('apple');
////GetJSONResult('mango');
////GetJSONResult('orange');
</script>
</head>
<body>
<br>
<div id="myDiv"></div>
<br>
</body>
</html>
答案 0 :(得分:1)
您是否注意到在if语句中您正在进行作业?
如果(N = -1)
应该是
if(n == -1)
或者如果你想真正安全
if(n === -1)