javascript - Set value of callback function nodejs -


socket.on('test', function (call, callback){      $('.button_1').on('click', function(){         callback('button_1 clicked');      });     $('.button_2').on('click', function(){         callback('button_2 clicked');     }); }); 

i don't know how set value of callback in situation. when delete first callback(); , keep second 1 or delete second 1 , keep first 1 code working fine when keep them both code doesn't work @ all

since callback argument function pass on(), responsibility of on() function determine callback.

based on reading of socket.io documentation: doesn't pass second argument @ all.

so need up.

socket.on('test', function (call, callback){                                    ^^^^^^^^ 

remove that. won't used, , mask callback in wider scope.

then add callback function elsewhere (i.e. in wider scope):

function callback(string_of_what_is_clicked) {     alert(string_of_what_is_clicked); } socket.on('test', function (call){      $('.button_1').on('click', function(){         callback('button_1 clicked');      }); // etc 

this clientside part , send callback server side tell them button clicked . there no relation between question , solution

if want send message client server when button clicked, don't on. use emit.

$('.button_1').on('click', function(){     socket.emit('button clicked', 'button_1 clicked');  }); 

and socket listens event:

socket.on('button clicked', function (msg) {     console.log('click: ' + msg); }); 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -