简体   繁体   English

如何在不退出javascript的情况下从javascript更新网页

[英]How to update a web page from javascript without exiting javascript

I would like to animate an html page with something like this: 我想用这样的动画HTML页面:

function showElements(a) {   
  for (i=1; i<=a; i++)  {  
    var img = document.getElementById(getImageId(i));  
    img.style.visibility = 'visible';  
    pause(500);  
  }  
}

function pause(ms) {
  ms += new Date().getTime();
  while (new Date() < ms){}
}

Unfortunately, the page only renders once javascript completes. 不幸的是,该页面仅在javascript完成后才呈现。

If I add 如果我加

 window.location.reload();

after each pause(500); 每次暂停后(500); invocation, this seems to force my javascript to exit. 调用,这似乎迫使我的JavaScript退出。 (At least, I do not reach the next line of code in my javascript.) (至少,我没有到达JavaScript的下一行代码。)

If I insert 如果我插入

 var answer=prompt("hello");

after each pause(500), this does exactly what I want (ie update of the page) except for the fact that I don't want an annoying prompt because I don't actually need any user input. 每次暂停(500)之后,除了我不需要讨厌的提示,因为我实际上不需要任何用户输入之外,这完全可以实现我想要的功能(即页面更新)。

So... is there something I can invoke after my pause that forces a refresh of the page, does not request any input from the user, and allows my script to continue? 那么...在暂停后是否可以调用某些内容来强制刷新页面,不请求用户任何输入并允许脚本继续运行?

While the javascript thread is running, the rendering thread will not update the page. 当javascript线程运行时,呈现线程将不会更新页面。 You need to use setTimeout . 您需要使用setTimeout

Rather than creating a second function, or exposing i to external code, you can implement this using an inner function with a closure on a and i : 无需创建第二个函数或将i暴露给外部代码,您可以使用内部函数在ai上加上闭包来实现此目的:

function showElements(a) {
    var i = 1;
    function showNext() {
        var img = document.getElementById(getImageId(i));  
        img.style.visibility = 'visible'; 
        i++;
        if(i <= a) setTimeout(showNext, 500);
    }
    showNext();
}

If I add window.location.reload(); 如果我添加window.location.reload(); after each pause(500) invocation, this seems to force my javascript to exit 每次pause(500)调用之后,这似乎迫使我的javascript退出

window.reload() makes the browser discard the current page and reload it from the server, hence your javascript stopping. window.reload()使浏览器丢弃当前页面并从服务器重新加载它,因此您的JavaScript停止了。


If I insert var answer=prompt("hello"); 如果我插入var answer=prompt("hello"); after each pause(500) , this does exactly what I want. 在每个pause(500) ,这正是我想要的。

prompt , alert , and confirm are pretty much the only things that can actually pause the javascript thread. promptalertconfirm几乎是实际上可以暂停javascript线程的唯一内容。 In some browsers, even these still block the UI thread. 在某些浏览器中,即使这些浏览器仍会阻塞UI线程。

Your pause() function sleeps on the UI thread and freezes the browser. 您的pause()函数在UI线程上休眠并冻结浏览器。
This is your problem. 这是你的问题。

Instead, you need to call setTimeout to call a function later. 相反,您需要调用setTimeout稍后再调用一个函数。

Javascript is inherently event-driven/non-blocking (this is one of the great things about javascript/Node.js). JavaScript本质上是事件驱动/非阻塞的(这是关于javascript / Node.js的伟大事情之一)。 Trying to circumvent a built in feature is never a good idea. 试图绕过内置功能绝不是一个好主意。 In order to do what you want, you need to schedule your events. 为了做您想做的事,您需要安排活动。 One way to do this is to use setTimeout and simple recursion. 一种方法是使用setTimeout和简单的递归。

function showElements(a) {   
  showElement(1,a);
}

function showElement(i, max) {
   var img = document.getElementById(getImageId(i));  
   img.style.visibility = 'visible';
   if (i < max) {
      setTimeout(function() { showElement(i+1, max) }, 500);
   }
}
var i = 1;
function showElements(a) {   
    var img = document.getElementById(getImageId(i));  
    img.style.visibility = 'visible';  
    if (i < a) {
      setTimeout(function() { showElements(a) }, 500);  
    }
    i++;
}
showElements(5);
function showElements(a,t) {   
  for (var i=1; i<=a; i++)  {  
    (function(a,b){setTimeout(function(){
      document.getElementById(getImageId(a)).style.visibility = 'visible'},a*b);}
    )(i,t)  
  }  
}

The t-argument is the delay, eg 500 t参数是延迟,例如500

Demo: http://jsfiddle.net/doktormolle/nLrps/ 演示: http//jsfiddle.net/doktormolle/nLrps/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM