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

Ste*_*anS 4 javascript greasemonkey

对于我来说,这个脚本正在为我工​​作,当某个网站被加载时,点击一个按钮.

但是我怎样才能设定等待时间?示例:加载网站并且脚本等待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
);
Run Code Online (Sandbox Code Playgroud)

脚本来源:如何让Greasemonkey点击仅在延迟后出现的按钮?

Dav*_*ern 13

这可能是您正在寻找的:

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

setTimeout(function() {

//Code to run After timeout elapses

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

}); 
Run Code Online (Sandbox Code Playgroud)