0

i have made an tag trigger function inside an anonymous function , here it is :

(function(){
  var getElement = {
    getElem: function(element , elemInterval){
      if(document.getElementsByTagName('div')[0].onload){
        element = document.getElementsByTagName('div')[0];
        clearInterval(elemInterval);
        element.innerHTML = ' content changed. ';
      }
    }
  }
  var element , elemInterval;
  elemInterval = setInterval(getElement.getElem(element , elemInterval) , 1000);
})();

what it got to do is to call a function as many time as it needs every one second and check if the first div as been loaded , than it save the element port to the "element" var and change the content of the div.

this seems to not work , what's the problem here?

3 Answers 3

1

You're calling it directly and passing the result value to setInterval, which is undefined.

What you probably want is passing a function to setInterval, which in turn will call getElem:

elemInterval = setInterval(function() {
                               getElement.getElem(element , elemInterval);
                           }, 1000);
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried it but it actually doing nothing , here is a live example : jsfiddle.net/NGnEd
@Mor Sela: That's because of your if clause. Why do you check for onload? jsfiddle.net/NGnEd/1
that's awesome man ! i though that the onload should give me true if it have been loaded , but i guess i was wrong , thank men.
@Mor Sela: No, you're checking whether the element has an onload="..." attribute, which is not really meaningful here.
1

You call the function immediately and pass undefined (its return value) as the first argument of setInterval. Learn how closures work.

elemInterval = setInterval(function() {
    getElement.getElem(element , elemInterval);
}, 1000);

1 Comment

I've tried it but it actually doing nothing , here is a live example : jsfiddle.net/NGnEd
0

The problem is most likely that of scope - the getElem function is contained in a var, which is contained in the function block (which has a scope of its own as well). Interval functions need to be accessible in the global scope.

I'm not able to test this right now, but iirc, this would work:

var something = (function(){
  var getElement = {
    getElem: function(element , elemInterval){
      if(document.getElementsByTagName('div')[0].onload){
        element = document.getElementsByTagName('div')[0];
        clearInterval(elemInterval);
        element.innerHTML = ' content changed. ';
      }
    }
  }
  var element , elemInterval;
  elemInterval = setInterval(something.getElement.getElem(element , elemInterval) , 1000);
  return getElement;
})();

But that's a high level of indirection, you might want to avoid containing your function in another nested function.

2 Comments

why you return this object in the end?
Because that's what gets assigned to 'var something' - without the return, something contains the function definition instead of the getElement object with the getElem method.

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.