Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. /*
  2.  
  3. //Example usage
  4.  
  5.  
  6. waitForElementToDisplay('button#pbid-ClearForm', function(){
  7. $("#pbid-ClearForm").replaceWith(function(){
  8. return $('<input/>', {
  9. id: "pbid-ClearForm",
  10. type: "submit",
  11. class: "pb-form-submit-button",
  12. value: "Clear Form",
  13. html: this.innerHTML
  14. });
  15. })
  16.  
  17. $('#pbid-ClearForm').click(function(e){
  18. e.preventDefault();
  19. //elementtooverwrite = '9';
  20. $('#pbid-GetBannerUserIDFormsubmit').click();
  21. $('#pbid-BannerUserID').val('');
  22. })
  23. });
  24.  
  25. */
  26.  
  27.  
  28. function waitForElementToDisplay(selector, callback, fallback=null, timeout=500, threshold=10, timer=null) {
  29. if(window.seconds == null) window.seconds = 0;
  30. if(timer == null) {
  31. var myint = setInterval(function(){
  32. window.seconds++
  33. }, 1000)
  34. } else myint = timer;
  35. if(document.querySelector(selector)!=null) {
  36. clearInterval(myint);
  37. if(typeof callback === "function") callback();
  38. return;
  39. }
  40. else {
  41. if(window.seconds < threshold){
  42. setTimeout(function() {
  43. waitForElementToDisplay(selector, callback, fallback, timeout, threshold, myint);
  44. }, timeout);
  45. } else {
  46. clearInterval(myint);
  47. if(fallback == null){
  48. fallback = function(){
  49. console.error('After ' +threshold+ ' seconds of attempting to locate the '+
  50. 'item with the selector of "' +selector+'", this item doesn\'t seem to exist.');
  51. }
  52. }
  53. if(typeof fallback === "function") {
  54. console.error('After ' +threshold+ ' seconds of attempting to locate the '+
  55. 'item with the selector of "' +selector+'", this item doesn\'t seem to exist. '+
  56. 'Your fallback function has been executed');
  57. fallback();
  58. }
  59. return;
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement