0

i have live file reading and i'm getting the files name from many links

name_container="<div><a class=\"friendclick\" data-frclick=\"$fr\" href=\"#\">$fullname</a></div>";

i need when (data-frclick) change -it changes on click- i need to pass it's value (targetread=dat) as (dat)

$('.friendclick').click(function(){
var dat=$(this).data('frclick');
});

i tried to put comet() function inside the click event function , it worked only when i click the link ,

but i couldn't get any live update from the file automatically, maybe if you can help me with making the (dat) global variable ,or any other suggestions !

$('.friendclick').click(function(){
 dat=$(this).data('frclick');

var timestamp = null;  //read
function comet() {

$.ajax({
    type : 'Get',
    url  : 'includes/read.php?timestamp=' + timestamp+'&targetread='+dat,
    async : true,
    cache : false,

    success : function(data) {
                var json = eval('(' + data + ')');
                if(json['msg'] == ''){
                    $('#msg').html('No msg');                   
                }else { 
                    $('#msg').html(json['msg']);
                         $('#msg').animate({scrollTop:$('#msg').get(0).scrollHeight},200);

                }
                timestamp  = json['timestamp'];
                setTimeout('comet()', 1000);
    },
    error : function(XMLHttpRequest, textstatus, error) { 
                //alert(error);
                setTimeout('comet()', 65000);
    }       
});
 }

 $(function() { // write
comet();
$('#send').bind('keyup', function(e) {
    var msg = $(this).val();
    if(e.keyCode == 13 && e.shiftKey) {  
        return ; 
    }else if(msg!='' && e.keyCode == 13) {
        $.ajax({
            type : 'GET',
            url  : 'includes/write.php?msg='+ msg.replace(/\n/g,'<br />')+'&target='+dat,
            async : true,
            cache : false
        });
        $(this).val('')
    }
});
 });

 });

and the php page for reading the file $target=$_GET['targetread']; here i got the target file then from the DB i get $file_name

    $filename = 'messaging/'.$file_name.'.txt';
$last = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$current = filemtime($filename);

while( $current <= $last) {
    usleep(100000);
    clearstatcache();
    $current = filemtime($filename);
}

$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = $current;
echo json_encode($response);
iiii
  • 23
  • 5
  • `var json = eval('(' + data + ')');` ***sigh*** Once you've eval'd it, it's not JSON anymore. JSON is a *textual* notation for data interchange. The result of parsing it is an object graph. (And using `eval` to parse JSON is a Bad Idea. Use `JSON.parse`, that's what it's for.) – T.J. Crowder May 09 '14 at 21:36
  • 2. Passing strings into `setTimeout` is an anti-pattern. Use `setTimeout(comet, 1000);` instead. – T.J. Crowder May 09 '14 at 21:37
  • What's the question? What behavior are you expecting, what do you see instead...? – T.J. Crowder May 09 '14 at 21:38
  • guys the code is working fine but i need to make the variable (dat) global – iiii May 09 '14 at 22:04

1 Answers1

0

Caution

I'll preface this answer with the statement that globals can be the cause of major headaches and confusion. Use them with care, if at all. See this question as to why.

Quick and Dirty

One (very hack-ish) option is to set a property of the window object.

Something like:

$('.friendclick').click(function(){
    var dat=$(this).data('frclick');
    window.myglobalvariable = dat;
});

Then you can reference it anywhere as window.myglobalvariable.

This probably isn't a very good solution, but without seeing how your code works at a higher level and understanding the scope of your ajax call and your click event, it's hard to make a better recommendation.

This will have the same effect as defining a variable without var:

$('.friendclick').click(function(){
    var dat=$(this).data('frclick');
    myglobalvariable = dat;
});

It's just a bit more clear as to what's happening.

Refactor

The other option is to re-factor your code such that you don't have to use a global variable. Depending on how your click event and comet() function are scoped, you might be able to do something like this:

$('.friendclick').click(function(){
    var mydata = $(this).data('frclick');
    comet(mydata);
});

function comet(data)
{
   ... code that makes ajax call with "data" as an argument ...
}
Community
  • 1
  • 1
Cypher
  • 2,608
  • 5
  • 27
  • 41
  • sadly didn't work , ok ,i'm gonna edit the question and i'll put the whole code – iiii May 09 '14 at 22:56
  • The `window` object is global by nature. Setting a property there will make your data global. If it "didn't work", please check your browser console, add some `alert()` and `console.log()` calls at various points in your code to make sure your data is moving along where it needs to. – Cypher May 09 '14 at 23:02
  • i tried it Uncaught ReferenceError: myglobalvariable is not defined – iiii May 09 '14 at 23:14
  • thank you , it was passing the comet() the variable dat – iiii May 09 '14 at 23:21