Link to home
Start Free TrialLog in
Avatar of rwniceing
rwniceing

asked on

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
Avatar of Gary
Gary
Flag of Ireland image

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

Open in new window

Avatar of rwniceing
rwniceing

ASKER

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
SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Any questions let me know
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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