2011-04-21 95 views
0

嘿,所有!有关用户显示和JavaScript的快速问题。我想要做的是关闭更新箭头gif,显示旋转的箭头,做更新,隐藏旋转的箭头,最后显示说这个项目现在设置的gif。通过三个循环循环 - 有些科室不改变

我的问题是旋转的箭头从不显示。我把睡眠放在那里,因为我认为更新发生得如此之快,以至于我没有看到它,但似乎并非如此。

任何帮助非常感谢。

function sleep(milliSeconds){ 
    var startTime = new Date().getTime(); // get the current time 
    while (new Date().getTime() < startTime + milliSeconds); // hog cpu 
} 

function UpdateConfig(some_id) { 
    /* loop through all divs of class .changed_dropdown and if visible do something */ 
    $(".changed_dropdown").each(function(index) { 
     if($(this).is(":visible")){ 

      some_id = ($(this).data('some_id')); 
      /* turn off the div that shows this is in need of update (uparrow gif) */ 
      $('#changed'+some_id).toggle(false); 
      /* turn on the div that displays the arrows going around in a circle (roundarrow gif) */ 
      $('#updating'+some_id).toggle(true); 

      /* pause to allow the divs to change */ 
      sleep(500); 
      var new_config = $("#" + some_id).val(); 
      $.post("change_config.php?some_id="+some_id+"&config="+new_config); 

      /* turn off the div that displays the arrows going around in a circle (roundarrow gif) */ 
      $('#updating'+some_id).toggle(false); 
      /* turn on the gif that indicates this is the current setting */ 
      $('#unchanged'+some_id).toggle(true); 

     } 
    }); 
} 

回答

1

只要把最后两行到传递进来的处理程序函数 “$。员额()”:

 $.post("change_config.php?some_id="+some_id+"&config="+new_config, {}, function() { 
     $('#updating'+some_id).toggle(false); 
     $('#unchanged'+some_id).toggle(true); 
    }); 

的 “$。员额()” 启动HTTP请求,但它会立即返回。通过推迟第二批切换,直到完成,您让其他事情发生,包括更新视图。

编辑 —另一个问题(我敢肯定)是,你用一个非常可疑的方式使用该参数“some_id”。它是“UpdateConfig()”的参数,但是你所做的只是从存储在一个元素上的某些数据重新分配它。但是因为它被声明为而不是函数顶部的“.each()”循环的处理函数,所以它在每次迭代中都被真正共享,因此也被所有这些“$ .post()”处理程序共享。我不知道为什么它是一个参数,但它应该是安全的,简单地改变该行地方它是从分配的“数据()”呼叫:

 var some_id = ($(this).data('some_id')); 

通过与var声明它,你能在一个这个“.each()”回调的局部变量,所以每个“$ .post()”处理程序都有它自己的副本。

+0

这确实得到了旋转箭头来显示,但现在处理程序中的切换不起作用。 – 2011-04-21 18:10:34

+0

好的,我想我明白了为什么发生这种情况 - 我会更新答案。 – Pointy 2011-04-21 18:16:25

+0

对于some_id你是对的,我还没有机会清理它。感谢您的提醒。尽管第二批切换仍然没有触发。 – 2011-04-21 18:56:02

2

如果您在代码中放置类似睡眠功能的文档DOM,则不会更新您的文档,它只会挂起浏览器。它只在脚本完成当前运行时更新。您必须使用setTimeout和异步操作并在回调函数中更新文档。