Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基本调用函数故障排除_Javascript_Jquery_Json_Function - Fatal编程技术网

Javascript 基本调用函数故障排除

Javascript 基本调用函数故障排除,javascript,jquery,json,function,Javascript,Jquery,Json,Function,如何获取函数checkViewers();正常工作?我认为我在JS文档文件中调用它是错误的,因此JS没有正确读取函数 当前代码: //Content Viewer Information function checkViewers() { //Base Variables var viewer = $('#viewed span.user'); var totalViews = $('#viewed span.user').length; var shortenViews =

如何获取函数checkViewers();正常工作?我认为我在JS文档文件中调用它是错误的,因此JS没有正确读取函数

当前代码:

//Content Viewer Information
function checkViewers() {

  //Base Variables
  var viewer = $('#viewed span.user');
  var totalViews = $('#viewed span.user').length;
  var shortenViews = $('#viewed span.user').length -1;

  if (totalViews === 0) {
      $('<span> 0 people have </span>').insertBefore($('#viewed span:last-child'));
  }
  if (totalViews === 2) {
      $('<span> and </span>').insertAfter(viewer.first());
  }
  if (totalViews >= 3) {
      viewer.slice(1).hide();
      $('<span> and </span>').insertAfter(viewer.first());
      $('<span class="user count"></span>').insertAfter(viewer.eq(2));
      $('.count').html(shortenViews + ' more people');
  }

}
//内容查看器信息
函数checkViewers(){
//基本变量
var viewer=$(“#已查看span.user”);
var totalViews=$('#viewsed span.user').length;
var shortenViews=$('#查看的span.user').length-1;
如果(totalViews==0){
$(“0人有”).insertBefore($(“#查看范围:最后一个孩子”);
}
如果(totalViews==2){
$('and').insertAfter(viewer.first());
}
如果(总视图>=3){
viewer.slice(1.hide();
$('and').insertAfter(viewer.first());
$('').insertAfter(viewer.eq(2));
$('.count').html(shortenViews+“更多人”);
}
}
在调用JSON数据之后,将在剩余JS的底部调用该函数

理想情况下,JSON数据应该输入到HTML中,并且函数应该捕获浏览者的数量。这将影响HTML中的查看器显示方式以及列出的查看器数量


查看。

将我的评论作为答案:

Ajax是异步的。你在网上订了一份比萨饼,然后在比萨饼送到你家之前就把它吃掉了。你必须等比萨饼出现再吃。AKA,在数据存在之前,不能调用函数。因此,当您设置html时,您可以调用您的函数

xhr.onload = function() {  //<--Function is called when the Pizza shows up at your door and the doorbell rings
     ...  //removed a bunch of code....
     //Update Page With New Content
      var viewerSection = $('#viewed');
      viewerSection.html(newViewers);  //You open up the pizza box
      checkViewers(); //<--- MOVE IT HERE, The pizza is here!!!!    
  }
};

xhr.open('GET', 'data.json', true);   //<-- You set up your pizza order
xhr.send(null);  //<--You make the pizza purchase 

//checkViewers();  <-- PIZZA IS NOT HERE!!!!

xhr.onload=function(){/回答我的评论:

Ajax是异步的。你从互联网上订购了一个比萨饼,并试图在比萨饼到达你家之前吃掉它。你必须等待比萨饼出现,然后才能吃掉它。也就是说,在数据存在之前,你不能调用你的函数。因此,当你设置html时,你调用你的函数

xhr.onload = function() {  //<--Function is called when the Pizza shows up at your door and the doorbell rings
     ...  //removed a bunch of code....
     //Update Page With New Content
      var viewerSection = $('#viewed');
      viewerSection.html(newViewers);  //You open up the pizza box
      checkViewers(); //<--- MOVE IT HERE, The pizza is here!!!!    
  }
};

xhr.open('GET', 'data.json', true);   //<-- You set up your pizza order
xhr.send(null);  //<--You make the pizza purchase 

//checkViewers();  <-- PIZZA IS NOT HERE!!!!

xhr.onload=function(){//在上面的代码中,您没有显示如何调用它。在随机站点上查看代码后,Ajax是异步的。您从internet订购了一个比萨饼,并尝试在比萨饼到达您家之前吃掉它。您必须等待比萨饼出现,然后才能食用它。也就是说,在数据存在之前,您不能调用您的函数。所以,when设置html,调用函数。@epascarello我喜欢你新的可视化方式asynch@charlietfl多年来一直在使用该示例:在上面的代码中,您没有显示如何调用它。在该随机站点上查看代码后,Ajax是异步的。您在internet上订购了一个比萨饼,并尝试在比萨饼到达之前吃掉它你的房子。吃比萨饼之前你必须等待它出现。也就是说,在数据出现之前你不能调用你的函数。所以当你设置html时,你可以调用你的函数。@epascarello我喜欢你新的可视化方式asynch@charlietfl多年来一直在使用这个例子:@epascarello您的类比在可视化Ajax方面肯定很有帮助。如果我发布一个示例,我通常会使用JSFIDLE。我使用Plunker的原因纯粹是因为我想从外部链接JSON,而不是从内部链接JSON。谢谢你的帮助!@epascarello你的类比在可视化Ajax方面肯定很有帮助。如果我发布一个示例,我通常会使用JSFIDLE。我使用Plunker的原因纯粹是因为我想链接JSON外部与内部的对比。感谢您的帮助!