【问题标题】:javascript function cannot called properlyjavascript函数无法正确调用
【发布时间】:2013-12-11 03:46:05
【问题描述】:
<html>
<head>
    <title>Get HTML code from any web page</title>
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.xdomainajax.js"></script>
    <script src="js/clientJs.js"></script>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen"/>
</head>
<body>
<div class="result"></div>
</body>
</html>

<?php
require_once('../connect.php');
error_reporting(-1);
function number_pad($number, $n)
{
    return str_pad((int)$number, $n, "0", STR_PAD_LEFT);
}

function GetLastChapter($id)
{
    $query = mysql_query("select*from tchapter where id_komik='$id' order by chapter desc limit 0,1") or die(mysql_error());
    $row = mysql_fetch_assoc($query);
    return $row['chapter']+1;
}
$manga_Array = array
(
    array("1","http://www.komikid.com","Naruto",GetLastChapter(1),"18"),
    array("4","http://www.komikid.com","One_Piece",GetLastChapter(4),"18")
);

foreach ($manga_Array as $manga) {
    print_r($manga); echo "<br>";
    $page = 0;
    $now = 1;
    while ($page < $manga[4]) {
        $page = number_pad($now, 2);
        $now++;

        $url = "$manga[1]/$manga[2]/$manga[3]/$page/";
        ?>
        <script>
            var myurl = '<?php echo $url ?>';
            setTimeout(function() {
                getHTMLContent(myurl,<?php echo $manga[0]; ?>,<?php echo $manga[3]; ?>);
            }, <?php echo $now * 5000 ?>);
        </script>
    <?php
    }
}
?>

上面有我用于从我的站点抓取图像的代码,但我认为它无法正常工作,每次调用 url 的函数 getHTMLcontent 总是相同的 url。对不起,我的英语不好,希望你们明白我的意思。

【问题讨论】:

  • @AllenChak :它 +1,我从 $now 获取 $page 值

标签: javascript php function loops


【解决方案1】:

setTimeout 调用的函数将引用同一个变量“myurl”。您可以将参数传递给函数:

<script>
var myurl;

myurl = 'URL 1';
setTimeout(function(url) {
    alert(url);    // 'URL 1'
}, 3000, myurl);

myurl = 'URL 2';
setTimeout(function(url) {
    alert(url);    // 'URL 2'
}, 6000, myurl);
</script>

【讨论】:

    【解决方案2】:

    您正在使用myurl 的全局变量。

    <script>
        setTimeout(function() {
            getHTMLContent('<?php echo $url ?>', <?php echo $manga[0]; ?>, <?php echo $manga[3]; ?>);
        }, <?php echo $now * 5000 ?>);
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2017-02-21
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      相关资源
      最近更新 更多