JavaScript - 强制行在Chrome中的循环内执行?

时间:2012-05-10 16:12:22

标签: javascript

我有一个while循环:

x = true;
while (x == true) {
   document.images['id'].src = arr[i];
   i = i+1;
   x = confirm('do you want to see more?')
}

这向我展示了每个图像,然后询问我是否想在firefox上看到更多内容,即在chrome和safari中,它只在我离开循环后显示图像。我知道这是有效的,但我想知道是否有办法在循环中强制执行循环中的行?

感谢您的投入!

3 个答案:

答案 0 :(得分:4)

您可以添加一系列setTimeout而不是循环来强制javascript用户线程停止,从而让浏览器刷新绘图。

var i = 0; // define outside showNextImage to be properly captured by it.
var showNextImage = function() {
    document.images['id'].src = arr[i]; 
    i = i+1; 
    x = confirm('do you want to see more?');
    if (true) setTimeout(showNextImage, 10);
};

答案 1 :(得分:2)

两个答案:

  1. 请勿使用confirm
  2. 如果您确实想使用confirm,请在更新图片后confirm
  3. 之前屈服于浏览器

    1。不要使用confirm

    最好的方法是根本不使用confirm;它是陈旧的,并且您发现它在不同浏览器上的行为方式略有不同,无论是否显示对页面的更改。

    相反,我会使用任何350,124个“对话框”库(jQuery UI有一个很好的,但同样有一个 批次 < / strong>其中),它们异步工作,因此您肯定会看到页面更改。你的循环将成为一个异步函数,但是一旦你习惯了它们并不是那么棘手,并且在用户体验方面的好处是巨大的。

    function chooseImage(arr, completionCallback) {
        var i = 0, imgElement = document.images['id'];
    
        ask();
    
        function ask() {
            imgElement.src = arr[i];
            showDialog(gotAnswer); // the nature of "show dialog" will depend on which one you use
        }
    
        function gotAnswer() {
            if (userSaidYes) { // Again, depends on the library you're using
                completionCallback(i); // Tell the calling code which one they picked
            }
            else {
                // Is there another?
                ++i;
                if (i >= arr.length) {
                    // No, tell the user
                    /* left as exercise */
    
                    // Tell calling code none was chosen
                    completionCallback(-1); // Using -1 as a flag for none
                }
                else {
                    // Yes, ask about it
                    ask();
                }
            }
        }
    }
    

    2。使用confirm但产量

    问题在于,当浏览器向用户询问问题时,confirm会使事情戛然而止。在确认窗口处于活动状态时(如您所见),您对页面所做的更改可能不会显示。

    如果你真的想使用confirm,你仍然可以这样做,只需先回到浏览器,以便有时间显示页面更改。 注意但是,如果图片需要很长时间才能下载,这仍然可能无法保证。

    function chooseImage(arr, completionCallback) {
        var i = 0, imgElement = document.images['id'];
    
        showAndHandOff();
    
        function showAndHandOff() {
            imgElement.src = arr[i];
            setTimeout(ask, 0);
        }
    
        function ask() {
            if (confirm('do you want to see more?')) {
                ++i;
                if (i >= arr.length) {
                    alert("Sorry, there aren't any more.");
                    completionCallback(-1);
                }
                else {
                    showAndHandOff();
                }
            }
            else {
                completionCallback(i);
            }
        }
    } 
    

答案 2 :(得分:0)

例如:

var x = true,  
  i = 0,  
  fn = function() {  
    document.images['id'].src = arr[i];  

    x = confirm('do you want to see more?');  
    if ( x ) {  
      i = i+1;  
      setTimeout(fn, 0)  
    }  
  };  
fn();