-1

I'm trying to write a js function that triggers another, variable, function when complete. I figured I could do this by passing the second function's name as a string, but I can't get it to work. Here's my code:

logMeIn('likeThisPost');

function logMeIn(callBack) {
    //First function content
        $.post("/ajaxurl/",
            {   
                login_id: login_id,
                intent: 'login'             
            },
            function(){
                console.log('Logged in, refreshing header');
                $.post("/secondajaxurl/",{},
                    function(data){
                    //success
                        if(typeof callBack!=='undefined') {
                            window[callBack]();
                            }
                        }
                    );
                }
            );      
        });
    }

This should, according to my thinking, run likeThisPost after successfully completing both ajax calls in logMeIn, but instead I get this error:

window[callBack] is not a function

The function I'm calling on success definitley exists, and besides which, it doesn't look like it's even trying to call that function, but it's treating callBack as literal rather than a string.

I'm using jQuery and have everything wrapped in a $. Where am I going wrong?

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
  • `I'm using jQuery and have everything wrapped in a $` that sounds like your problem. For `window[callback]()` to work, the function named in `callback` needs to be declared in global scope, not under a jquery document.ready handler. It would make far more sense to actually pass the function reference in anyway, instead of cobbling together the function name from the `window` reference. – Rory McCrossan Feb 05 '18 at 16:16
  • 1
    `logMeIn(likeThisPost)` maybe? – Jonas Wilms Feb 05 '18 at 16:16
  • Where did you declare the `function likeThisPost()`? This should work (I think) if you declared the function on the root and not inside any function like `$(document).ready(function(){` – Eddie Feb 05 '18 at 16:17
  • 1
    I believe you created your `likeThisPost` function outside of window scope. – Ahmet Can Güven Feb 05 '18 at 16:19
  • @JonasW. - I also had to call it directly rather than going through `window` (which was how I was directed to do it in every tutorial!). – Sinister Beard Feb 05 '18 at 16:29
  • You should definetly change your tutorial source. This is just ugly. – Jonas Wilms Feb 05 '18 at 16:30
  • @JonasW. It was from StackOverflow! https://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string – Sinister Beard Feb 05 '18 at 16:40

1 Answers1

0

With thanks to Jonas W and Rory McCrossan, the answer was to change the passed function to the function name, rather than a string, and then call the function directly rather than using window.

So logMeIn(likeThisPost) instead of logMeIn("likeThisPost") and callBack(); rather than window[callBack]();.

Thanks!

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95