最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setTimeout doesn't seem to work in Chrome - Stack Overflow

programmeradmin2浏览0评论

This setTimeout works perfectly in Firefox, but in Chrome nothing in function timeoutTrigger ever happens, including the alert. 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)

This setTimeout works perfectly in Firefox, but in Chrome nothing in function timeoutTrigger ever happens, including the alert. 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)
Share Improve this question asked Jan 4, 2013 at 3:45 maskedjellybeanmaskedjellybean 7192 gold badges9 silver badges18 bronze badges 10
  • 3 Is the first 2 lines in timeoutTrigger causing some exception? Is there any error messages in console? the code seems correct. – otakustay Commented Jan 4, 2013 at 3:48
  • 2 +1 otakustay, this seems to work jsfiddle.net/sASvm – Tuanderful Commented Jan 4, 2013 at 3:48
  • 2 Isn't this a keyword that shouldn't be used as a variable? – Felix Guo Commented Jan 4, 2013 at 3:49
  • 1 @MrXenotype : the variable is $this, not this – Pranav 웃 Commented Jan 4, 2013 at 3:51
  • 1 In Chrome, Developer Tools is your friend -- either F12 or CTRL+Shift+I. Check the console for error messages. – mike Commented Jan 4, 2013 at 4:03
 |  Show 5 more comments

3 Answers 3

Reset to default 9

Switch your setTimeout statement to the following: 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. Please read: https://developer.chrome.com/extensions/contentSecurityPolicy

We had trouble with settimeout function on Chrome. We wanted to run a API check in every 10 seconds to update page content if there is change in data. But although we set settimeout function to run in every 10 seconds, Chrome does not run it correctly if browser is inactive. (While Safari runs the code correctly even if it is inactive).

Our experiment shows that Chrome runs our function about minutely or a little longer when it is inactive. So checking updates minutely does not broke our code.

But we had second function which runs every minutes and changes content locally every minutes. 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 first time, and check the minutes passed between dates, and change the waiting times and playing times accordingly.

     var $first_run_time; 
     var $minutes_passed = 0;
     var $total_minutes_used = 0;
     var $interval = 10000; // 10 seconds

     $current_date = new Date();
     $first_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_from_start = Math.floor((Math.abs($current_date - $first_run_time)/1000)/60);

            $minutes_passed = $minutes_passed_from_start - $total_minutes_used;
  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());

    $total_minutes_used += $minutes_passed;

      }
     }
发布评论

评论列表(0)

  1. 暂无评论