Closure example for javascript-1, callback function

Dear Experts,

For setTimeinterval()  that can do callback function such as

window.setInterval(displaymesg,2000, "message is here");
function displaymesg(mesg){
document.getElementById("message-wrapper").innerHTML+= mesg;
}

Open in new window


The code above  is working for all browsers except older version of IE browser.

The question is
1-it works  for the browsers becoz they are using javascript closure to pass argument to the callback function, Right ?
2- it didn't work for IE old browser bcoz they did NOT use javascript closure to do that ,Right?
3- If item-1 is correct, they are using similar or concept closure javascript code as follows, Right ?

var displaymesg=closurefunc("message is here");
window.setInterval(displaymesg,2000);
function closurefunc(mesg){
    function callbackfunc(){
 document.getElementById("message-wrapper").innerHTML+=mesg;
}
return callbackfunc;
}

Open in new window


Or there is other method besides closure to pass the argument into callback function ?

Please advise

Rwniceing
LVL 1
rwniceingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Correct
setInterval(function(){
    displaymesg("msg here")
}, 2000);

Open in new window

0
rwniceingAuthor Commented:
Gary, you mean

1-Yes,
2-Yes, old browser did not use closure
3-Yes, similar concept code as attachment above(just for concept only)

All "yes" ,Right ?

Last question before closing this thread , why older browser didn't use closure ? Is it becoz closure feature implemented  in latest version of browser from suggestion from latest ECMA script specification ?
In other words, in the past there is no closure concept or thing in javascript


Please advise
0
GaryCommented:
setInterval (and the similar setTimeout) are from pre ECMA standards, so browsers could interpret and implement them however they liked
These days all browsers operate (almost) to the ECMA standards

I don't have time to find the docs on it at the mo.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rwniceingAuthor Commented:
Gary, your code seems different from window.setInterval(displaymesg,2000, "message is here");

If you code is corrrect, we don't need to use closure on setInterval since it can pass
"msg here" into displaymesg function argument directly from your code without any closure code help, Right ? If so, the item-1 question is not correct .

setInterval(function(){
    displaymesg("msg here")
}, 2000);

Open in new window

0
RobOwner (Aidellio)Commented:
Not so much to do with closure but as Gary indicated about how the browsers interpreted the pre ECMA standard, so their implementation was different.

To reiterate Gary's code
var msg = "message is here";
window.setInterval(function() {
	displaymesg(msg);
},2000);

function displaymesg(mesg){
	document.getElementById("message-wrapper").innerHTML+= mesg;
};

Open in new window


http:#a40278995 There is no closure here.  The setInterval function is defined as:
http://www.w3schools.com/jsref/met_win_setinterval.asp

Mozilla: https://developer.mozilla.org/en/docs/Web/API/window.setInterval, which talks about how closures are avoided
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobOwner (Aidellio)Commented:
Any questions let me know
0
RobOwner (Aidellio)Commented:
https://developer.mozilla.org/en/docs/Web/API/window.setInterval
Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer 9 and below. If you want to enable this functionality on that browser you must use a compatibility code (see the Callback arguments paragraph).

Callback Arguments:
https://developer.mozilla.org/en/docs/Web/API/window.setInterval#Callback_arguments
0
rwniceingAuthor Commented:
In other words,

Answer to Question on the topic post
1-NO, there is no need to use closure to do the setInterval()  for those browser works for setInterval since it is specified from http://www.w3schools.com/jsref/met_win_setinterval.asp
2-Yes, for old browser such IE older version, setInterval is not working  since IE is not support passing back the callback agrument, we can use the closure to solve it as previous code attached OR use other technique such as
 IE-specific compatibility code found from https://developer.mozilla.org/en/docs/Web/API/window.setInterval
3- Correct, if IE is not working for old browser for setInterval, or read item-2 above

I think I concluded those my question with the the asnwer above that is okay, Right ?

Finally question is I heard some IE is not working setInterval with passing argument, Is it right for older version of IE or latest version of IE ? I 'm confusing that.
0
RobOwner (Aidellio)Commented:
Older versions you can, new versions do not. You can't pass the variable the way you were going.
What you've assumed above is correct
0
rwniceingAuthor Commented:
Thanks for all of reply

Answer to Question on the topic post
1-NO, there is no need to use closure to do the setInterval()  for those browser works for setInterval since it is specified from http://www.w3schools.com/jsref/met_win_setinterval.asp and from from pre ECMA standards
2-Yes, for IE laterst version, setInterval is not working  since IE is not support passing back the callback argument, we can use the closure to solve it as previous code attached OR use other technique such as IE-specific compatibility code found from https://developer.mozilla.org/en/docs/Web/API/window.setInterval
3- Correct and  if IE laterst version is not working  for setInterval, or read item-2 above
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.