简体   繁体   中英

Node.js call function if button clicked

I have a function in my index.ejs which will be executed if you click on an image.

img.onclick = function() {playVideo();}

A Video will play, which works fine.

But now I want to get a "notification" in node.js that the function was called / image was clicked. I don't want to send back data or get a var. I just want a function which will be executed in node.js.

I work with node.js and express.js on a raspberry pi.

Look for further infos in the comments.

I would suggest you to call the function via a dummy post request.The video will continue playing and the the function will start running in the server

In server

   router.post("/call",(req,res,next)=>{<call your function here>});

Hope it helps cheers

Pass function as a parameter

Example : Create a following method

OnSuccess(){
console.log('Event Happened Successfully');
}

img.onclick = function(OnSuccess) {playVideo(OnSuccess);}

// In your play video method

playvideo(OnSuccess){
//Your play video code
OnSuccess(); // this will call OnSuccess method in line 1
}

you have to send an ajax request from your UI to the back end to inform it that something happened (button clicked, video loaded, ....). In ejs you can not pass node js function like this for example:

<%=updateButtonClicked();%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM