rwniceing
asked on
Closure example for javascript-1, callback function
Dear Experts,
For setTimeinterval() that can do callback function such as
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 ?
Or there is other method besides closure to pass the argument into callback function ?
Please advise
Rwniceing
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;
}
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;
}
Or there is other method besides closure to pass the argument into callback function ?
Please advise
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gary, your code seems different from window.setInterval(display mesg,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 .
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Any questions let me know
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Open in new window