设置等待时间,延迟时间,执行脚本

时间:2015-09-12 09:38:23

标签: javascript greasemonkey

这个fatmonkey脚本正在为我工​​作,点击一个按钮,当某个网站被加载时。

但我该如何设定等候时间?示例:网站已加载,脚本等待1秒钟,直到执行完毕。

我的第二个问题是:每个页面加载我怎么能只运行一次?该脚本一遍又一遍地开始。

    // ==UserScript==
// @name     _YOUR_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

function clickSubmitBtnWhenItAppears (jNode) {
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

//-- Value match is case-sensitive
waitForKeyElements (
    //"#btn_submit input[type='submit'][value*='Click Me Now']",
    "input[type='submit'][value*='Click Me Now']",
    clickSubmitBtnWhenItAppears
);

脚本来源:How do I get Greasemonkey to click on a button that only appears after a delay?

1 个答案:

答案 0 :(得分:11)

这可能就是你要找的东西:

$(document).ready(function() { //When document has loaded

setTimeout(function() {

//Code to run After timeout elapses

}, 2000); //Two seconds will elapse and Code will execute.

});