简体   繁体   中英

What am I doing wrong here - setTimeout on document load

Are working with an app and need to execute the function postCook after 10s but something is wrong. Just worked with Javascript / jQuery for a couple of day's so still not flooowing :)

But have now been stuck on this for several hours so even if it breaks my heart i need to ask for advice

$(document).ready(function() { setTimeout("postCook", 10000);
};

  function postCook()
  {
  FB.api(
  '/me/app:action',
  'post',
  { game: '<?php echo get_permalink(); ?>' },

function(response) {
if (!response || response.error) {
alert('Error occurred');
} else {
alert('Successful! Action ID: ' + response.id);
}
    });
         }

Can execute it with button without problem so 100% sure that it's within setTimeout

Remove the quotes. setTimeout(postCook,5000) . NEVER pass a string to setTimeout .

The problem with your code is that you never call the function. The code in the string just gets the reference to the function and does nothing with it. This would work:

setTimeout("postCook()", 10000);

However, as you have a reference to a function, you should use that directly instead:

setTimeout(postCook, 10000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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