简体   繁体   English

setTimeout 在 Chrome 中似乎不起作用

[英]setTimeout doesn't seem to work in Chrome

This setTimeout works perfectly in Firefox, but in Chrome nothing in function timeoutTrigger ever happens, including the alert.此 setTimeout 在 Firefox 中完美运行,但在 Chrome 中,function timeoutTrigger 中没有任何事情发生,包括警报。 Any ideas?有任何想法吗?

var $this = $('.active-more');

function timeoutTrigger() {
    $this.closest(".container").nextAll(".container:first").find(".description:first").removeClass('hide');
    $this.closest(".container").nextAll(".container:first").find(".back:first").find("img.portfolio").remove();
    alert("is this thing on?");
}

setTimeout(function(){timeoutTrigger()},400)

Switch your setTimeout statement to the following: setTimeout(timeoutTrigger,400); setTimeout语句切换为以下内容: setTimeout(timeoutTrigger,400); The one you wrote is for when the function you're calling has a parameter. 你写的那个是你正在调用的函数有一个参数。 Also, you're missing a semicolon. 另外,你错过了一个分号。

Google has changed the Content Security Policy which impacts using setTimeout() in some browsers. 谷歌已经改变了在某些浏览器中使用setTimeout()影响的内容安全策略。 Please read: https://developer.chrome.com/extensions/contentSecurityPolicy 请阅读: https//developer.chrome.com/extensions/contentSecurityPolicy

We had trouble with settimeout function on Chrome.我们在 Chrome 上遇到了 settimeout function 的问题。 We wanted to run a API check in every 10 seconds to update page content if there is change in data.如果数据发生变化,我们希望每 10 秒运行一次 API 检查以更新页面内容。 But although we set settimeout function to run in every 10 seconds, Chrome does not run it correctly if browser is inactive.但是,尽管我们将 settimeout function 设置为每 10 秒运行一次,但如果浏览器处于非活动状态,Chrome 将无法正确运行它。 (While Safari runs the code correctly even if it is inactive). (而 Safari 即使它处于非活动状态也能正确运行代码)。

Our experiment shows that Chrome runs our function about minutely or a little longer when it is inactive.我们的实验表明,当 Chrome 处于非活动状态时,Chrome 会运行我们的 function 大约一分钟或更长时间。 So checking updates minutely does not broke our code.因此,仔细检查更新不会破坏我们的代码。

But we had second function which runs every minutes and changes content locally every minutes.但是我们有第二个 function,它每分钟运行一次,并且每分钟更改一次本地内容。 To be specific, we change waiting times and play times of games which we are showing in the page content.具体来说,我们更改页面内容中显示的游戏的等待时间和播放时间。 So as we can not depend on Chrome to run at exact minutes, our solution is to save when the code is run last time, and check the minutes passed between dates, and change the waiting times and playing times accordingly.因此,由于我们不能依赖 Chrome 在准确的分钟数上运行,我们的解决方案是保存上次运行代码的时间,并检查日期之间经过的分钟数,并相应地更改等待时间和播放时间。

 var $last_run_time; 
 var $interval = 10000; // 10 seconds

 $current_date = new Date();
 $last_run_time = $current_date;
 console.log('Timeout started '+ $current_date.toLocaleTimeString());
 setTimeout(my_refresh_function, $interval));

function my_refresh_function() {
        setTimeout(my_refresh_function, $interval);
        $current_date = new Date();
        console.log('Refresh run: '+ $current_date.toLocaleTimeString());
        $minutes_passed = Math.floor((Math.abs($current_date - $last_run_time)/1000)/60);

  if ($minutes_passed > 0 ) {
       // Make the changes you want for passed minutes
       // for example increase play time $play_time += $minutes_passed
       // and reduce waiting times $waiting_time -= $minutes_passed
        console.log('Minutes updated +- '+ $minutes_passed + ' mins, at: ' + $current_date.toLocaleTimeString());

  }
 }

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

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