• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 570
  • Last Modified:

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
0
rwniceing
Asked:
rwniceing
  • 4
  • 4
  • 2
4 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now