safari的“window.autoRefresh”的替代方案是什么?

时间:2017-05-22 07:43:45

标签: javascript safari settimeout page-refresh

在safari中window.autoRefresh不会被触发。如何使这个工作?我得到window.autoRefresh的任何例子和相同的文档。

此代码自动刷新div而不是页面

if(window.autoRefresh==true)//without this condition, refresh works fine.
        {
            if (typeof autoRefreshTimeout == 'undefined'){
            autoRefreshTimeout = setTimeout(function(){
                    clearTimeout(autoRefreshTimeout);
                    autoRefreshTimeout = undefined;
                    dosomething();
                    }

                }, 30000);
            }

1 个答案:

答案 0 :(得分:1)

没有财产window.autoRefresh。如果需要,可以将其设置为自定义属性。如果您想在几毫秒后刷新多次,请使用setInterval代替setTimeout

var autoRefreshTimeout;
var count = 0;
    
// function that change div content
function dosomething() {
  document.getElementById('counter').innerHTML = count;
  count++;
}

// start timeout/interval
function startRefresh() {
  // check if already refreshing
  if(!window.autoRefresh) {
    // set custom property
    window.autoRefresh = true;

    // autoRefreshTimeout = setTimeout(function() {
    autoRefreshTimeout = setInterval(function() {
      console.log('refresh');
      dosomething();
    }, 1000);
  }
}

// stop timeout/interval
function stopRefresh() {
  if (window.autoRefresh) {
    // set custom property
    window.autoRefresh = false;

    // clearTimeout(autoRefreshTimeout);
    clearInterval(autoRefreshTimeout);
  }
}
<button onclick='startRefresh()'>start</button>
<button onclick='stopRefresh()'>stop</button>
<div id="counter"></div>