简体   繁体   English

为什么不起作用? 的JavaScript

[英]Why doesn't this work? JavaScript

It's a greasemonkey script. 这是油腻的猴子脚本。 I don't know javascript so that makes this especially fun. 我不懂javascript,所以这特别有趣。

function notify(){
    if window.find("1 minute")==true{
        alert("y");
        setTimeout("alert('time to click soon!')",60000)
    }
    if window.find("2 minute")==true{
        alert("y");
        setTimeout("alert('time to click soon!')",2*60000)
    }

...repeated x30... ...重复x30 ...

}
notify();

Thanks in advance. 提前致谢。

You're missing parentheses around the if conditionals. 您在if条件周围缺少括号。 They should look like: 它们应该看起来像:

if (window.find("1 minute")) {
    alert("y");
    setTimeout("alert('time to click soon!')",60000)
}

Also, no need to explicitly test against true ; 同样,无需显式测试是否为true the if statement does that for you! if语句为您做到了!


More things to make Douglas Crockford less angry with you 更多让Douglas Crockford对您生气的东西

Don't pass strings into setTimeout . 不要将字符串传递给setTimeout Pass functions. 传递函数。 Like this: 像这样:

setTimeout(function () {
    alert('time to click soon!');
}, 60000);

or like this (even better, since you're reusing the function): 或类似这样(甚至更好,因为您正在重用该函数):

function showAlert() {
    alert('time to click soon!');
}

// later...

setTimeout(showAlert, 60000);

另外,我认为您可能应该使用unsafeWindow

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

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