Solved

Javascript with callback

Posted on 2011-03-01
2
276 Views
Last Modified: 2012-05-11

Hi

I have a simple piece of example code, in which I am passing a function (for call back) into a class.
When this function is called back, I wish to have a parameter passed back but this is not working.

Thanks for you help
Phil
<?php
  
?>

<!DOCTYPE html>
<head>

<script language='javascript' type='text/javascript'>
window.onload=init;

function init() {
    var y;
    var myClassObj = new MyClass(5, calledBack); 
}

function calledBack(x) {
    alert("Got called back with "+x);
}

    
function MyClass(val, f)
{
    var data = val;
    
    function someFunc(x) 
    {
       alert("someFunc "+x);
       f.call(x);
    }
    
    someFunc(data);
}
</script>


</head>
<body>

<div style="" >

</div>

</body>
</html>

Open in new window

0
Comment
Question by:BeerFizz
2 Comments
 
LVL 11

Accepted Solution

by:
jasonduan earned 500 total points
ID: 35012289
change your code line #28 from
f.call(x);
to
f(x);
0
 

Author Closing Comment

by:BeerFizz
ID: 35012321
Perfect!  Thank you.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question