JAVA script doubt

in this particular code, why do i have to write Function (){alert("Hello}; and can you please explain this whole code included within tags

which code? plz mention the project number or name or give a picture of code plz

Hello,

This program is used to demonstrate the function of setTimeout() function.

When the button (Hello guys button) is clicked, the say_hello() function is called.

In this say_hello() function, we use the setTimeout() function (which is a built in javascript function) we pass two parameters to the setTimeout() function, a function and time interval.

  1. first parameter: we must pass a function as the first parameter. So we create a function here itself which has an alert() method to display a popup box saying “Hello”. (alert() method is used to display a pop-up box/dialogue box which is used for alerting/notifying something )

function()
{
alert(“Hello”);
}

  1. second parameter: we must specify the interval of time (in milliseconds) after which the function should execute. The output of this function will be displayed after the interval of time specified, rather than immediately.

3000

the complete setTimeout function according to the program will be:

setTimeout(function(){ alert(“Hello”); } , 3000);

Thus the dialogue/pop-up box will appear after 3000 milliseconds i.e. 3 seconds after the button “Hello guys” is clicked

The order of execution:

“Hello guys” button is clicked,
say_hello() function is called which has setTimeout() function inside it,
because of the setTimeout() function the pop-up box/dialogue box having the content “Hello” will appear 3 seconds after the button was clicked.

Hope I helped in clearing your doubt.

2 Likes

Hello, thanks for sharing solution about Java script it really works for me because I want to make career in JAVA so I start to learn.To know more click: https://www.cetpainfotech.com/technology/java-training

function is the keyword in Javascript that is used to create functions.
Statements are enclosed within a function to repeatedly execute it or to reuse the bunch of statements for a particular task.

the syntax / structure of a function in javascript is as follows :
function name_of_the_function()
{
//statements
}
setTimeout() is a predefined function which usually takes 2 arguments: function name and milliseconds
this function is used to call a function or evaluate an expression after the amount of milliseconds mentioned as arguments.

The above code you asked to explain creates a function named say_hello
The setTimeout() takes arguments :
(i) First a function that displays an alert message “Hello”
(ii) 3000 milliseconds (that is 3 seconds).

Hence when you call the say_hello() function it’ll display an alert message “Hello” exactly after 3 seconds.

To know more about functions, refer to https://www.w3schools.com/js/js_functions.asp
to know more about the setTimeout() predefined function, refer to https://www.w3schools.com/jsref/met_win_settimeout.asp

Hope this solved your doubt :slight_smile:

can you tell me what is the name of " function(){ alert(“Hello”); }" this function and how it is working. I am not able to understand it.
@hibafatima24
@Bharati