简体   繁体   English

我在这里做错了什么-文档加载时的setTimeout

[英]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. 正在使用某个应用程序,需要在10s后执行postCook函数,但是出了点问题。 Just worked with Javascript / jQuery for a couple of day's so still not flooowing :) 刚使用Javascript / jQuery进行了两天的工作,所以仍然不会麻烦:)

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 可以用按钮执行它而没有问题,因此100%确保它在setTimeout内

Remove the quotes. 删除引号。 setTimeout(postCook,5000) . setTimeout(postCook,5000) NEVER pass a string to setTimeout . 切勿将字符串传递给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);

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

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