0

can you help me?

I'm using a script in one file to call other function in another file, but I'm getting function is not defined. So I've start searching to fix this way but I didn't get any positive results. Check my code below:

index.html

$(document).ready(function(){
    setInterval(function(){
        lAs = new loadAsync();
    }, 900);
});


js/plugins.js

function window.loadAsync() {
    $(".loadAsync").each(function(index, element){
        $(element).attr("src", $(element).attr("data-src"));
    });
}

So, what should I do to fix this problem? Thank you.

@edit:

<script src="js/plugins.js"></script>

I've already wrote this in index.html

4
  • function loadAsync() { Commented May 18, 2018 at 22:54
  • 1
    What is this function window.loadAsync() way of declaring functions ? You should be getting a SyntaxError. Commented May 18, 2018 at 22:54
  • I saw it on another thread in stackoverflow, but with it or not, the error persists: Uncaught ReferenceError: loadAsync is not defined Commented May 18, 2018 at 22:57
  • If it's still not defined, then there's some problem that isn't represented in your question. Commented May 18, 2018 at 23:10

2 Answers 2

3

First, your syntax is wrong. An identifier may not have a . in it. Just use loadAsync and it'll be global unless you've nested it in another scope.

You can also make the jQuery more efficient by calling .attr() with a callback instead of using .each().

Finally, it seems unlikely that you actually want to use new to invoke the function. I changed the code below to simply pass it as the callback to setInterval. I also declared the lAs variable properly.

function loadAsync() {
    $(".loadAsync").attr("src", function(){
        return $(this).attr("data-src");
    });
} 

var lAs;
$(document).ready(function(){
    lAs = setInterval(loadAsync, 900);
});

And of course all this can be done easily without jQuery.

function loadAsync() {
  for (const el of document.querySelectorAll(".loadAsync")) {
    el.src = el.dataset.src;
  }
} 

var lAs;
document.addEventListener("DOMContentLoaded", function(){
    lAs = setInterval(loadAsync, 900);
});
Sign up to request clarification or add additional context in comments.

6 Comments

Downvoted because you most likely downvoted my answer while essentially repeating the same thing in yours. If the reasoning is different, please do inform me and I will remove my downvote.
Thanks. IMO, things would be so much better if they just allowed votes to be public.
@Crazy_Train Yep, downvotes certainly should be harder, I bet that there are certain people who don't even view question details and just downvote based on bad titles, therefore throwing half the new questions into an abyss, but I hear that they are making the site from friendly towards newcomers.
@doubleOrt also I didn't downvoted you too, I haven't level to perform this action.
@CrazyTrain I'm getting this warning/error now: prntscr.com/jjqqur
|
0

First of all, function window.whatever() {} is wrong (could you include the link of the SO thread where you saw that being used ?).

Since you are getting an error even without that, please make sure that:
1. loadAsync is declared in the global scope in the plugins file.
2. You are including js/plugins.js correctly. Go on and insert foo bar; in js/plugins.js and then check if you get a SyntaxError (while including it before the other JS file), if you don't, you are not correctly including it.

1 Comment

I saw yesterday, I don't have the link right now. Sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.