2014-03-06 109 views
0

我有一个脚本,显示一个cookie值throw document.write。如何每隔30秒刷新一次div?

如何刷新脚本 - 每30秒更新一次?或者,如果它是不可变的,再次获取cookie值。 value = GetCookie('VisitorName');

或者,把它放在iframe中并重新加载iframe scr。

代码如下:

var expDays = 30; 
    var exp = new Date(); 
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000)); 

    function Who(info) { 
     var VisitorName = GetCookie('VisitorName') 

     if (VisitorName == null) { 
      VisitorName = "Dear visitor"; 
      SetCookie ('VisitorName', VisitorName, exp); 
     } 

     return VisitorName; 
    } 

    function set() { 
     VisitorName = prompt(""); 
     SetCookie ('VisitorName', VisitorName, exp); 
    } 

    function getCookieVal (offset) { 
     var endstr = document.cookie.indexOf (";", offset); 

     if (endstr == -1) 
      endstr = document.cookie.length; 

     return unescape(document.cookie.substring(offset, endstr)); 
    } 

    function GetCookie (name) { 
     var arg = name + "="; 
     var alen = arg.length; 
     var clen = document.cookie.length; 
     var i = 0; 

     while (i < clen) { 
      var j = i + alen; 
      if (document.cookie.substring(i, j) == arg) 
     return getCookieVal (j); 
      i = document.cookie.indexOf(" ", i) + 1; 
      if (i == 0) 
       break; 
     } 

     return null; 
    } 

    function SetCookie (name, value) { 
     var argv = SetCookie.arguments; 
     var argc = SetCookie.arguments.length; 
     var expires = (argc>2) ? argv[2] : null; 
     var path = (argc >3) ? argv[3] : null; 
     var domain = (argc >4) ? argv[4] : null; 
     var secure = (argc >5) ? argv[5] : false; 
     document.cookie = name + "=" + escape (value) + 
      ((expires == null) ? "" : (";expires=" + expires.toGMTString())) + 
      ((path == null) ? "" : (";path=" + path)) + 
      ((domain == null) ? "" : (";domain=" + domain)) + 
      ((secure == true) ? ";secure" : ""); 
    } 

    function DeleteCookie (name) { 
     var exp = new Date(); 
     exp.setTime (exp.getTime() - 1); 

     var cval = GetCookie (name); 
     document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString(); 
    } 

    document.write("" + Who() + ",") 

回答

2

我想你想的setTimeoutsetIntervalfunctions here

function myFunc() { 
    alert('Hi') 
} 
setTimeout(myFunc, 30000) // 1000 ms = 1 second 

所以我不知道你想每隔30秒重复一次有什么功能,但它可能是这个样子:

function MyTimerFunction() { 
    document.write("" + Who() + ",") 
} 
MyTimerFunction(); // runs MyTimerFunction immediately 
setInterval(MyTimerFunction, 5000) // run MyTimerFunction it every 5 seconds 
+0

它重新加载,但显示结果干净的页面上。它将新的值添加到旧的值,所以每个时间间隔有更多的值值!我想留在内容页面上并删除以前的值。也许我必须将它插入这样的标签? – user3381580

+0

框架很少是要走的路。您需要在页面上创建一个div元素并将函数写入它。很容易,但没有整个HTML代码很难快速解释,而不知道你的技能水平。看看[接受的答案在这里](http://stackoverflow.com/questions/15119856/alternative-way-to-use-javascript-to-write-to-a-div-element)如何写入一个DIV。这将取代'document.write(“”+ Who()+“,”)'行。 –

+0

我写,再次同样的结果 user3381580

相关问题