如何使javascript变量全局,并将其传递到函数ajax

how to make javascript variable global and pass it into function ajax?

本文关键字:ajax 函数 javascript 何使 变量 全局      更新时间:2023-09-26

我有实时文件读取我正在从许多链接中获取文件名

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

我需要当(data-frclick)改变-它改变点击-我需要传递它的值(targetread=dat)作为(dat)

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

我试图把彗星()函数内部的点击事件函数,它只工作,当我点击链接,

但是我无法自动从文件中获得任何实时更新,也许如果你能帮助我制作(dat)全局变量,或任何其他建议!

$('.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('')
    }
});
 });
 });

和用于读取文件的PHP页面$目标= $ _GET [' targetread '];这里我得到了目标文件然后从数据库我得到$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);

注意事项

在回答这个问题之前,我先声明全局变量可能会导致严重的头痛和混乱。如果有的话,要小心使用。看这个问题,为什么。

Quick and Dirty

一个(非常hack的)选项是设置window对象的属性。

类似:

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

那么你可以在任何地方引用它作为window.myglobalvariable

这可能不是一个很好的解决方案,但是如果没有看到您的代码在更高级别上是如何工作的,并且没有了解ajax调用和单击事件的范围,很难提出更好的建议。

这将与定义一个没有var的变量具有相同的效果:

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

这样可以更清楚地看到发生了什么。

重构

另一个选择是重构你的代码,这样你就不必使用全局变量了。根据单击事件和comet()函数的作用域,您可以执行如下操作:

$('.friendclick').click(function(){
    var mydata = $(this).data('frclick');
    comet(mydata);
});
function comet(data)
{
   ... code that makes ajax call with "data" as an argument ...
}