Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
uiwebview javascript不返回变量,但返回警报_Javascript_Ios_Uiwebview - Fatal编程技术网

uiwebview javascript不返回变量,但返回警报

uiwebview javascript不返回变量,但返回警报,javascript,ios,uiwebview,Javascript,Ios,Uiwebview,我在javascript中有这个函数: function getResponse(){ setInterval(function(){ value = player.getCurrentTime(); getValue(value); },100); } function getValue(value){ var lines = value; //alert("Value is: " + lines); return li

我在javascript中有这个函数:

function getResponse(){
    setInterval(function(){
        value = player.getCurrentTime();
        getValue(value);
    },100);
}
function getValue(value){
    var lines  = value;
    //alert("Value is: " + lines);
    return lines;
}
当我想要得到lines变量时,我没有得到任何响应

NSString *response = [self.webView stringByEvaluatingJavaScriptFromString:@"getResponse()"];
NSString *resonse2 = [self.webView stringByEvaluatingJavaScriptFromString:@"getValue();"];

NSLog(@"oh %@ %@",response,resonse2);
但是,如果我解除警报,警报将返回正确的响应。我不知道我需要做什么

谢谢

function getResponse(){
    setInterval(function(){
        value = player.getCurrentTime();
        getValue(value);
    },100);
}
因此,此函数在100毫秒后异步调用匿名函数。 100毫秒后,它将
值设置为当前时间,并调用
getValue()
我不知道
value
来自何处,但我会假设它在范围内(可能是全局的)

这个函数基本上只返回给定的值。我真的不知道JS的作用域规则,但是当你从iOS调用它时,你没有传递任何东西,所以可能它得到的是前一个(gloabl?
value
变量的值

那么我们在iOS中做什么呢?

NSString*response=[self.webView stringByEvaluatingJavaScriptFromString:@“getResponse()”];
NSString*Resons2=[self.webView stringByEvaluatingJavaScriptFromString:@“getValue();”;

首先,我们调用
getResponse()
,这将在100ms后启动对
getValue()
的异步调用

然后,我们立即自己调用
getValue()
,不带任何参数,并返回响应

但是,当显示警报时,它会工作。我的JS知识有点欠缺,但我猜这是因为显示警报占用了等待100毫秒所需的时间。然后,在警报完成时,(全局?)变量
值已由
getResponse()
填充,这意味着它返回值

我们如何解决这个问题? 同步进行所有这些有什么问题吗?我们必须有两个功能吗? 我最初的想法是,为什么我们不能运行这个:

function getCurrentTime() {
    return player.getCurrentTime();
}

您是否返回了行,但它在哪里分配?您正在调用getValue()函数,而不给变量或返回语句赋值。
function getCurrentTime() {
    return player.getCurrentTime();
}