Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- //Example usage
- waitForElementToDisplay('button#pbid-ClearForm', function(){
- $("#pbid-ClearForm").replaceWith(function(){
- return $('<input/>', {
- id: "pbid-ClearForm",
- type: "submit",
- class: "pb-form-submit-button",
- value: "Clear Form",
- html: this.innerHTML
- });
- })
- $('#pbid-ClearForm').click(function(e){
- e.preventDefault();
- //elementtooverwrite = '9';
- $('#pbid-GetBannerUserIDFormsubmit').click();
- $('#pbid-BannerUserID').val('');
- })
- });
- */
- function waitForElementToDisplay(selector, callback, fallback=null, timeout=500, threshold=10, timer=null) {
- if(window.seconds == null) window.seconds = 0;
- if(timer == null) {
- var myint = setInterval(function(){
- window.seconds++
- }, 1000)
- } else myint = timer;
- if(document.querySelector(selector)!=null) {
- clearInterval(myint);
- if(typeof callback === "function") callback();
- return;
- }
- else {
- if(window.seconds < threshold){
- setTimeout(function() {
- waitForElementToDisplay(selector, callback, fallback, timeout, threshold, myint);
- }, timeout);
- } else {
- clearInterval(myint);
- if(fallback == null){
- fallback = function(){
- console.error('After ' +threshold+ ' seconds of attempting to locate the '+
- 'item with the selector of "' +selector+'", this item doesn\'t seem to exist.');
- }
- }
- if(typeof fallback === "function") {
- console.error('After ' +threshold+ ' seconds of attempting to locate the '+
- 'item with the selector of "' +selector+'", this item doesn\'t seem to exist. '+
- 'Your fallback function has been executed');
- fallback();
- }
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement