`
ablya
  • 浏览: 26381 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

刷新父窗口和JavaScript页面时延总结

阅读更多
http://home.phpchina.com/space.php?uid=92279&do=blog&id=54762


最近学习JavaScript碰到了些问题,但还好,最终都解决了,现做一个简单的总结,望对大家有帮助。
1、首先碰到的是模态对话框刷新父窗口的问题,我只对知识点列举下:
使用window.open()弹出的弹出窗口,刷新父窗口

window.opener.location.reload()

使用window.showModalDialog弹出的模式窗口

window.dialogArguments.location.reload();其中dialogArguments是父窗口传递给模态对话框的参数,用这条刷新语句的前提是必须将window(代表父窗口本身)作为第二个参数传递给对话框,例如:

var NewWind = showModalDialog('Url', window [, features])

2、刷新窗口有以下几种方法:

   1    window.history.go(0)
   2    window.location.reload()
   3    window.location=location
   4    window.location.assign(location)
   5    window.document.execCommand('Refresh')
   6    window.window.navigate(location)
   7    window.location.replace(location)
   8    window.document.URL=location.href

3、自动刷新页面的方法:
1.页面自动刷新:把如下代码加入<head>区域中
<meta http-equiv="refresh" content="20">
其中20指每隔20秒刷新一次页面.

2.页面自动跳转:把如下代码加入<head>区域中
<meta http-equiv="refresh" content="20;url=http://www.wyxg.com">
其中20指隔20秒后跳转到http://www.wyxg.com页面

3.页面自动刷新js版
   <script language="JavaScript">
   function myrefresh()
   {
        window.location.reload();
   }
   setTimeout('myrefresh()',1000); //指定1秒刷新一次
   </script>自动刷新页面的方法:
   1. 页面自动刷新:把如下代码加入<head>区域中
      <meta http-equiv="refresh" content="20">
       其中20指每隔20秒刷新一次页面.

    2.页面自动跳转:把如下代码加入<head>区域中
    <meta http-equiv="refresh" content="20;url=http://www.wyxg.com">
     其中20指隔20秒后跳转到http://www.wyxg.com页面

    3.页面自动刷新js版
    <script language="JavaScript">
    function myrefresh()
    {
          window.location.reload();
    }
    setTimeout('myrefresh()',1000); //指定1秒刷新一次
    </script>

4、页面时延的方法也很多,网上也有很多中,这里我只说自己比较看好的一种:

   <script language="javascript"> 
/*Javascript中暂停功能的实现 
Javascript本身没有暂停功能(sleep不能使用)同时 vbscript也不能使用doEvents,故编写此函数实现此功能。 
javascript作为弱对象语言,一个函数也可以作为一个对象使用。 
比如: 
function Test(){ 
alert("hellow"); 
this.NextStep=function(){ 
  alert("NextStep"); 


我们可以这样调用 var myTest=new Test();myTest.NextStep(); 

我们做暂停的时候可以吧一个函数分为两部分,暂停操作前的不变,把要在暂停后执行的代码放在this.NextStep中。 
为了控制暂停和继续,我们需要编写两个函数来分别实现暂停和继续功能。 
暂停函数如下: 
*/ 
/*利用window.eventList系统对象来传递Test这个弱对象,这是由于你的函数有可能是带参数的。

由面向对象的思想,传递参数尽量不要采用全局变量,因为你的对象有可能有1个也有可能有n个,而

有些时候所创建对象的个数并不是你事先可以知道的,那么要创建全局变量的个数自然很难判断了。

所以此处用一个中间载体来传递对象,而不是参数值!*/


function Pause(obj,iMinSecond){ 
if (window.eventList==null)
{
  //alert("window.eventList="+window.eventList);
   window.eventList=new Array();
}
var ind=-1; 
for (var i=0;i<window.eventList.length;i++){
//alert("window.eventList.length="+window.eventList.length); 
   if (window.eventList[i]==null) { 
     window.eventList[i]=obj; 
     ind=i; 
     break; 
    } 

if (ind==-1){ 
   ind=window.eventList.length;
//alert("ind="+ind); 
   window.eventList[ind]=obj;
//alert("obj"+obj.name); 

setTimeout("GoOn(" + ind + ")",iMinSecond); 

/* 
该函数把要暂停的函数放到数组window.eventList里,同时通过setTimeout来调用继续函数。 
继续函数如下: 
*/ 

function GoOn(ind){ 
var obj=window.eventList[ind]; 
//alert("GoON obj"+obj);
window.eventList[ind]=null; 
if (obj.NextStep) obj.NextStep(); 
else obj(); 

/* 
该函数调用被暂停的函数的NextStep方法,如果没有这个方法则重新调用该函数。 
函数编写完毕,我们可以作如下册是: 
*/ 
function Test(){ 
alert("hello"); 
Pause(this,2000);//调用暂停函数 
this.NextStep=function(){ 
  alert("NextStep"); 


&lt;/script>
当然,setTimeout()和setInterval()也是两个不错的函数,特别有用,需要注意的是前者有三个参数,第一个是延时后要调用的函数引用或各种表达式,第二个参数是一个整数,表示延时时间,单位微秒,第三个参数是可选的,当第一个参数是函数时它便是传递给这个函数的参数,当时延条件满足时就执行第一个参数指定的动作;而后者与前者不同的是在时间间隔内循环一直调用,而且调用的这个函数引用(第一个参数)不能用引号括起来,并且不能加括号,例如:

function toDo()

{....}

setTimeout('toDo()', 1000);

setInterval(toDo, 1000);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics