Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I found this code posted on a website and I am trying to understand how it works:
JavaScript
function myConfirm(dialogText, okFunc, cancelFunc, dialogTitle) {
  $('<div style="padding: 10px; max-width: 500px; word-wrap: break-word;">' + dialogText + '</div>').dialog({
    draggable: false,
    modal: true,
    resizable: false,
    width: 'auto',
    title: dialogTitle || 'Confirm',
    minHeight: 75,
    buttons: {
      OK: function () {
        if (typeof (okFunc) == 'function') {
          setTimeout(okFunc, 50);
        }
        $(this).dialog('destroy');
      },
      Cancel: function () {
        if (typeof (cancelFunc) == 'function') {
          setTimeout(cancelFunc, 50);
        }
        $(this).dialog('destroy');
      }
    }
  });
}

This is how it is implemented:
JavaScript
myConfirm('Do you want to delete this record ?', function () {
    alert('You clicked OK');
  }, function () {
    alert('You clicked Cancel');
  },
  'Confirm Delete'
);


What I have tried:

I am hoping someone here can explain how it works, line by line.
Posted
Updated 17-Sep-21 8:46am

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Fair enough. So here is my first question:

How does the script tie the parameters "okFunc" and "cancelFunc" to their related button, as relates to the first line in the script?

JavaScript
function myConfirm(dialogText, okFunc, cancelFunc, dialogTitle) {


TD
 
Share this answer
 
Comments
Richard Deeming 20-Sep-21 5:28am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under the solution and post a comment.

Do not post your comment as a "solution" to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900