GreaseMonkey 倒计时不起作用?

发布于 2024-09-07 15:19:23 字数 870 浏览 3 评论 0原文

我很困惑,greasemonkey setTimeout 不起作用,它从不调用该函数,在网上查看人们说greasemonkey 不支持 setTimeout,有没有办法让我的目标(如下)工作?

function countdown(time, id) {
   if(document.getElementById(id)) {
       var name = document.getElementById(id);
       var hrs = Math.floor(time / 3600); 
       var minutes = Math.floor((time - (hrs * 3600)) / 60); 
       var seconds = Math.floor(time - (hrs * 3600) - minutes * 60);

       if(hrs>0) {
            name.innerhtml = hrs + 'h ' + minutes + 'm';
       } else if(minutes>0) {
            name.innerhtml = minutes + 'm ' + seconds + 's';
       } else {
            name.innerhtml = seconds + 's';
       }
   } else {
       setTimeout('countdown(' + --time + ',' + id + ')', 100);
   }

   if(time <= 0)
      window.location.reload();
   else
      setTimeout('countdown(' + --time + ',' + id + ')', 1000);
} 

I'm very confused, with greasemonkey setTimeout just isn't working, it never calls the function, looking online people say greasemonkey doesn't support setTimeout, is there anyway to make my objective (below) work?

function countdown(time, id) {
   if(document.getElementById(id)) {
       var name = document.getElementById(id);
       var hrs = Math.floor(time / 3600); 
       var minutes = Math.floor((time - (hrs * 3600)) / 60); 
       var seconds = Math.floor(time - (hrs * 3600) - minutes * 60);

       if(hrs>0) {
            name.innerhtml = hrs + 'h ' + minutes + 'm';
       } else if(minutes>0) {
            name.innerhtml = minutes + 'm ' + seconds + 's';
       } else {
            name.innerhtml = seconds + 's';
       }
   } else {
       setTimeout('countdown(' + --time + ',' + id + ')', 100);
   }

   if(time <= 0)
      window.location.reload();
   else
      setTimeout('countdown(' + --time + ',' + id + ')', 1000);
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-09-14 15:19:23

问题出在setTimeout的文本参数上。它与 Greasemonkey 配合得很好,但如果您使用文本命令而不是回调,则代码永远不会执行,因为在 setTimeout 触发时,greasemonkey 沙箱已被清除。它尝试使用文本参数 wchis 运行 eval ,然后尝试调用当时已不存在的函数 countdown

目前程序流程如下:

1. function countdown(){}

2. setTimeout("countdown()", 1000);

3. clearGreasemonkeySandbox();

4. ... wait 1 sec...

5. eval("countdown()"); // <- countdown doesn't exist anymore

因此您应该使用回调,这样使用指向函数的指针而不是完整的句子。

setTimeout(function(){
    countdown(--time, id);
}, 1000);

The problem lies in the textual parameter of setTimeout. It works very well with greasemonkey but if you use textual commands instead of callbacks, the code is never executed since greasemonkey sandbox is cleared by the time the setTimeout fires. It tries to run eval with the textual parameter wchis in turn tries to call function countdown which doesn't exist by that time anymore.

Currently the program flow is as follows:

1. function countdown(){}

2. setTimeout("countdown()", 1000);

3. clearGreasemonkeySandbox();

4. ... wait 1 sec...

5. eval("countdown()"); // <- countdown doesn't exist anymore

So you should use callbacks instead, this way a pointer to a function is used instead of the full sentence.

setTimeout(function(){
    countdown(--time, id);
}, 1000);
緦唸λ蓇 2024-09-14 15:19:23

最后我最终使用了

window.setTimeout(bla, 1000);

window.bla

= function() {alert("cool"); }

In the end I ended up using

window.setTimeout(bla, 1000);

and

window.bla = function() { alert("cool"); }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文