-2

One javascript file that my webpage loads has this call

window.setTimeout(function() {
    self.MakeWaterFall(self)
}, 500);

That .js file is hosted on other server and is loaded on webpage similar like many js libs from CDNs are loaded,, and that file has 'hardcoded' setTimeout call after 500ms

Now, I'd like that 'MakeWaterFall' function to run little later on a page

I'd like it to run after custom trigger fires, after ajax data comes down the wire and renders posts on a page.

This is that binded trigger that I need to 'move' MakeWaterFall fn to..

$(window).bind( 'grid:items:added' , function(){
  // .. some custom events on elements +
  MakeWaterFall(window);
} ) ;

this question in essence is like 'can I hook up before window.setTimeout runs and block one spec call and move it somewhere else, latter in the code' ?

also not that I have other bunch of code that runs in other places in code via window.setTimeout code,, I know that sucks :(

thanks

Kresimir Pendic
  • 3,597
  • 1
  • 21
  • 28
  • 1
    why don't you call MakeWaterFall() in the callback in the function you want to execute first – marvel308 Aug 04 '17 at 13:39
  • 1
    Are both functions asynchronous? If they're synchronous they will run one after another. If not, use a callback. – Andrew Li Aug 04 '17 at 13:39
  • So what does this other function look like? Can't just just call self.MakeWaterfall at the end of it, or wrap it into another function that calls the two? Or is there some timer or asynchronous stuff going on? In the latter case, you really need to provide more context. – GolezTrol Aug 04 '17 at 13:39
  • @marvel308 I can't make callback directly, see my updated question.. – Kresimir Pendic Aug 04 '17 at 19:23
  • @GolezTrol I have updated question,, see if that makes more sense now.. I tried to make it more understandable.. – Kresimir Pendic Aug 04 '17 at 19:25

2 Answers2

0

use a callback-function like this:

function myFunction(callback){

myFunction1();

if(typeof(callback) === "function()"){

myFunction2();

}}"

LeoScript
  • 19
  • 2
0

ok I found a solution and it's very simple actually .. it incudes this steps:

1) I made sure that I loaded my custom.js file earlier on the page, before hosted CDN 'problematic' js file comes on page (that has setTimeout in it)

2) I preserved native function and made my custom one and did this:

// save original setTimeout function to savedST
window.savedST = window.setTimeout ;
// define custom window.setTimeout function and hook to it
window.setTimeout = function(a, b){ 
    // I'm testing if this calls waterfall fn.. then bounce that f() to other binded event
    if ( a.toString().indexOf( 'MakeWaterFall' ) > -1 ){
        $(window).bind( 'grid:items:added', function(){
            a();
        });
    } else {
        // run normal or original setTimeout as usual
        savedST(a, b);  
    }
} ;

I'm sure this is something that will help someone that will need to override "setTimeout" and/or "setInterval", native window functions

cheers, k

Kresimir Pendic
  • 3,597
  • 1
  • 21
  • 28