Jump to content

AJAX send and recive info from php


westman

Recommended Posts

thescientistthe chat coding has been done, am just working on this last bug, it is not a generle chat room, it is more like face book 1 on 1 chat. justsomeguyam trying to understand your meening of timer ID. i have use after...function text_info(theword) {var timer = clearTimeout("text_info('"+theword+"')");clearTimeout(timer);these 2 with no luck i do not know the ID of...var timer = setTimeout("text_info('"+theword+"')",5000); how do i target the timer to clear it out?

Link to comment
Share on other sites

  • Replies 58
  • Created
  • Last Reply

just read the tutorials. any timer function (interval or timeout) returns an ID to reference that particular instance of a timer.http://www.w3schools...leartimeout.asp

var timer = setTimeout(someFunctionReference, someDelayTime);////clearTimeout(timer);

Link to comment
Share on other sites

force of habit, since I don't use global variables in my scripts unless it's an object I use for namespacing. but if you take var out it will make it global, and then it will be available everywhere. It is important to understanding when and why you should use var, as it is a pretty fundamental concept in the language, and using var is considered a best practice. you should just post your code with what you tried so we can actually see what you've tried. It's hard to tell just by being told.

Link to comment
Share on other sites

You only use var when you are declaring a variable. In the example you linked to they declared all of their variables at the top using the var keyword. Since the variables are declared outside of any of the functions that means they are global variables and they can be accessed inside any function. That means that one function can set the variable and another function later can read the variable and it will have the same value that was set in the other function. If you use var to declare a variable inside a function then it is not global, once the function ends the variable and its value get destroyed. If you want a variable to hold a global timer ID then you need to declare it first outside of the function like the example shows. You can set the timeout in one function call and clear it in a later one. You never use the var keyword to refer to a variable, only to declare it.

Link to comment
Share on other sites

ok we are geting closer and am understanding more & more about javascriptso here is what i have...

function text_info( theword) {  output = document.getElementById("text_output");var timer = clearTimeout(timer);clearTimeout(timer);var timer = "";  var update = "update"  var new1 = <?php echo $new1 ?>;  var new2 = theword;  var url = "test.php";  //alert('sending request to ' + url + ' with update="' + update + '"; new1="' + new1 + '"; new2="' + new2 + '"');  $.post(url,{ update: update, new1: new1, new2: new2 } , function(data) {    //alert('got response from server, updating page with ' + data);    output.innerHTML = data;  });  //alert("scheduling text_info('"+theword+"') to run in 5 seconds");  var timer = setTimeout("text_info('"+theword+"')",5000); // every 1000 = 1 sec, so 5000 = 5sec}

in this code i am trying to remove all info about...var timer = setTimeout("text_info('"+theword+"')",5000);as soon a the function runs. then at the end of the function build the timer again.i used...var timer = clearTimeout(timer);clearTimeout(timer);var timer = "";but with no luckhow can i debug this to work? how can i clear out all info on the timer when the function runs?

Link to comment
Share on other sites

OMG i got it working ;) am so pleased :good: so here is my script (working)

timer = "";function text_info( theword) {  output = document.getElementById("text_output");clearTimeout(timer);  var update = "update"  var new1 = <?php echo $new1 ?>;  var new2 = theword;  var url = "test.php";  $.post(url,{ update: update, new1: new1, new2: new2 } , function(data) {    output.innerHTML = data;  });  timer = setTimeout("text_info('"+theword+"')",5000); // every 1000 = 1 sec, so 5000 = 5sec}

the code is makeing a globle verible named timer at the end of the function. at the start on the function globle verible timer is cleard and for the script to run the 1st time i named the globle velible timer outside the function and set it to nothing ;) last question.how does the sctipt look in working terms? any mistaks that stand out?

Link to comment
Share on other sites

Been looking at this and i still don't know exactly what you are looking for?

var timer = "";function text_info( theword) {clearTimeout(timer);output = document.getElementById("text_output");  var update = "update"  var new1 = <?php echo $new1 ?>;  var new2 = theword;  var url = "test.php";  //alert('sending request to ' + url + ' with update="' + update + '"; new1="' + new1 + '"; new2="' + new2 + '"');  $.post(url,{ update: update, new1: new1, new2: new2 } , function(data) {    //alert('got response from server, updating page with ' + data);    output.innerHTML = data;  });  //alert("scheduling text_info('"+theword+"') to run in 5 seconds");  timer = setTimeout("text_info('"+theword+"')",5000); // every 1000 = 1 sec, so 5000 = 5sec}

Link to comment
Share on other sites

you post number 29 was purfect but only 1 difrencevar timer = ""; and timer = "";i did not know about globle veriblse so i did not know the difrence betwen them. all in all i only whated to remove all info of the timer every time the function runs. at 1st when the scriped was not working i thoght it was been appended to, but it was just runing more than 1 timer at the same time. now that only 1 timer is runing at a time the script is working very well. i would like to thank, justsomeguyfor helping me understand more about veribles in javascript thescientistfor been patient and advising in time of need. but most of all i will like to thank dsonesukfor script righting because without them i would not know where to start. thank you all for your time so mush i can know sleep at night. you may look at this script and think, that is so easy i could do it in 5 min and yes you could, but for us noobs in javascript it could take 1 year to debug ;) again thank you.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...