Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set waiting time, delay time, to execute script

this script for greasemonkey is working for me, to click a button, when a certain website was loaded.

But how can I set a waiting time? Example: The website loaded and the script waits 1 second till it is executed.

My second question is: how can I run it only once per pageload? The script starts over and over again.

    // ==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
);

Script source: How do I get Greasemonkey to click on a button that only appears after a delay?

like image 274
StefanS Avatar asked Sep 12 '15 09:09

StefanS


People also ask

How do you add a delay to time in HTML?

let timeoutID = setTimeout(function, delay in milliseconds, argument1, argument2,...); The delay is set in milliseconds and 1,000 milliseconds equals 1 second. If the delay is omitted from the setTimeout() method, then the delay is set to 0 and the function will execute.

How do you sleep 5 seconds in JavaScript?

import { setTimeout } from "timers/promises"; const yourFunction = async () => { await setTimeout(5000); console. log("Waited 5s"); await setTimeout(5000); console.


1 Answers

This might be what you are looking for:

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

setTimeout(function() {

//Code to run After timeout elapses

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

}); 
like image 50
David Davern Avatar answered Oct 23 '22 00:10

David Davern