Skip to content

Instantly share code, notes, and snippets.

@8dcc
Created April 17, 2022 02:41
Show Gist options
  • Save 8dcc/2c595e5cf6ff4288b9ea19ba5ff3ec59 to your computer and use it in GitHub Desktop.
Save 8dcc/2c595e5cf6ff4288b9ea19ba5ff3ec59 to your computer and use it in GitHub Desktop.
Add custom delay to userscript functions.
// ==UserScript==
// @name Delay function
// @namespace Violentmonkey Scripts
// @include *://example.com/*
// @run-at document-start
// @author Null (r4v10l1)
// ==/UserScript==
// --------------------- Settings ---------------------
var MY_DELAY = 500; // ms, delayChecker will wait this
var MY_INITIAL_DELAY = 500; // ms, the script will wait this before the first check
// ----------------------------------------------------
function mainFunction() {
console.log("This is the main function to be executed.")
}
function delayChecker(customDelay) {
// Remove after you test the userscript!
console.log("Entered the checker! You can test the userscript by typing 'condition = true' on the console.");
if (condition) mainFunction();
else setTimeout(function () { delayChecker(customDelay); }, customDelay);
}
condition = false;
setTimeout(function () { delayChecker(MY_DELAY); }, MY_INITIAL_DELAY);
@8dcc
Copy link
Author

8dcc commented Apr 17, 2022

You are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment