perl 异步和js对比;
/js**************
$(function(){
function isPhone(str){
var regex = /[0-9]{11,11}/;
return regex.test(str);
}
var _bt = $(".bt");
_bt.on("click",function(){
var tel = $("[name='tel']").val();
var env = $("[name='env']").val();
if(isPhone(tel)){
var data = {};
data.phone = tel;
data.env = env;
var successfn = function(jdata){
if(jdata[0][2] != undefined){
$(".gridtable").empty();
var _HTML="<tr><th>ENV </th><th>TEL</th><th>Code</th></tr>";
$.each(jdata,function(i,val){
_HTML=_HTML+"<tr><td>"+env+"</td><td>"+tel+"</td><td>"+jdata[i][0]+"\n"+jdata[i][1]+"</td></tr>";
});
$(".gridtable").html(_HTML);
}else{
$(".gridtable").empty();
var _HTML="<tr><th>ENV </th><th>TEL</th><th>Code</th></tr>";
_HTML = _HTML+"<tr><td>"+env+"</td><td>"+tel+"</td><td>"+jdata[0][1]+"</td></tr>";
$(".gridtable").html(_HTML);
}
};
var errorfn = function(jdata){
alert("数据返回出错");
};
$.axg(
"/DEVOPS/index.php/Codequery/getcode",
data, ##data表示请求接口的参数
successfn,
errorfn
);
}else{
alert("输入的手机号码不符合规范");
}
});
});
/perl***********
use AnyEvent::HTTP;
use HTTP::Date qw(time2iso str2time time2iso time2isoz);
my $cv = AnyEvent->condvar;
http_request GET => 'http://120.55.118.6:3000/api/env?ip=192.168.32.101',timeout => 20, sub {
my ($body, $hdr) = @_;
$time2=time2iso(time());
print "\$time2 is $time2\n";
print "$body\n";
};
$time1=time2iso(time());
print "\$time1 is $time1\n";
print "11111111111111111111\n";
$cv->recv();
异步请求的结果 http_request GET => 'http://120.55.118.6:3000/api/env?ip=192.168.32.101' 返回给函数
sub {
my ($body, $hdr) = @_;
$time2=time2iso(time());
print "\$time2 is $time2\n";
print "$body\n";
};
再来看一个奇葩的perl 异步调用:
#!/usr/bin/perl
use AnyEvent;
use AnyEvent::HTTP;
my $cv = AnyEvent->condvar;
sub doit{
my $url = shift ;
return if not defined $url;
$cv->begin;
http_get( "$url", sub { done( $url, @_ ) } );
print "1111111111111111\n";
}
sub done {
my ($url, $content, $hdr) = @_;
$cv->end();
print "Search: $url\tStatus: ", $hdr->{Status}, "\n";
print "\$content is $content\n";
};
&doit('http://120.55.118.6:3000/api/env?ip=192.168.32.101');
print "222222222222222222\n";
$cv->recv();
&doit('http://120.55.118.6:3000/api/env?ip=192.168.32.101');
传递的参数为http://120.55.118.6:3000/api/env?ip=192.168.32.101
http_get( "$url", sub { done( $url, @_ ) } ); 异步请求的结果 传递给函数 sub { done( $url, @_ ) } ,结果传递给函数done
转载于:https://www.cnblogs.com/zhaoyangjian724/p/6199742.html