Javascript RandomNum未定义,我没有';我不明白为什么

Javascript RandomNum未定义,我没有';我不明白为什么,javascript,Javascript,我从中获取了这个脚本教程,我尝试结合这个类似教程中的随机函数,只是设置了它,它说RandomNum是未定义的。为什么,我该如何修复它 Javascript: /*** LET'S ADD THE VARIABLE SETTINGS. TO TOGGLE USE TRUE/FALSE ***/ var domainURL = window.location.hostname; var className = "default"; var postCount = 3; var s

我从中获取了这个脚本教程,我尝试结合这个类似教程中的随机函数,只是设置了它,它说RandomNum是未定义的。为什么,我该如何修复它

Javascript:

/*** LET'S ADD THE VARIABLE SETTINGS. TO TOGGLE USE TRUE/FALSE ***/
var domainURL = window.location.hostname;
var className = "default";
var postCount = 3;
var showPostLabel = "test";
var showPostContentAmount = 160;
var showPostTitle = "true";
var showPostTitleAmount = 80;
var showInPostImageSize = 320;
var showPostComments = "true";
var showPostDate = "true";
var showPostAuthor = "true";
var showPostImage = "true";
var showPostCategory = "true";
var showPostTags = "true";
var showPostContent = "true";
var showCategoryArchive = "true";
var showRandomPosts = "true";

/*** LET'S GENERATE RANDOM POSTS ***/
if (showRandomPosts == 'true')
{
  var RandomArray = [];
  var TotalPosts  = 0;
  var RandomArray = new Array(postCount);
  
  function TotalCount(json)
  {
    TotalPosts   = json.feed.openSearch$totalResults.$t;
  }
  document.write('<script src="https://'+domainURL+'/feeds/posts/default?alt=json-in-script&callback=TotalCount"></'+'script>');

  function GenerateNum()
  {
    for (var i = 0; i < postCount ; i++)
    {
      for (var j = 0; j < RandomArray.length; j++)
      {
        var RandomNum = Math.floor(Math.random() * (TotalPosts ) + 1);
        RandomArray[i] = RandomNum; 
      }
    }
  }
}

/*** LET'S START THE POST LIST ***/
function jsonposts(json)
{
  document.write('<ul class="json-posts '+className+'">');
  for (var i = 0; i < postCount; i++)
  {
    /*** LET'S DECLARE VARIABLES ***/
    var postVariables= postImage = postUrl = postTitle = postImage = postContent = postArticle = postAuthor = postLabel = postDate = postUpdated = postComment = postImageURL = postCategoryArchive = postImageURLSize = postAuthorImage = postDateFormat = Y = D = M = m = YY = DD = MM = mm = TT =  "";

    /*** LET'S GET THE POST LABELS ***/
    if (json.feed.entry[i].category != null)
    {
      for (var k = 0; k < json.feed.entry[i].category.length; k++) 
      {
        postLabel += '<a class="json-post-article-tags-item" href="'+domainURL+'/search/label/'+json.feed.entry[i].category[k].term+'">'+json.feed.entry[i].category[k].term+'</a>';
        if(k < json.feed.entry[i].category.length-1)
        { 
          postLabel += '<span class="json-post-article-tags-item-seperator">, </span>';
        }
      }
    }

    /*** LET'S GET THE POST URL ***/
    for (var j = 0; j < json.feed.entry[i].link.length; j++) 
    {
      if (json.feed.entry[i].link[j].rel == 'alternate') 
      {
        break;
      }
    }
    postUrl= '"' + json.feed.entry[i].link[j].href + '"';

    /*** LET'S GET THE POST INFO ***/
    postCategoryArchive = json.feed.openSearch$totalResults.$t;

    /*** LET'S GET THE POST TITLE ***/
    if (json.feed.entry[i].title!= null)
    {
      postTitle= json.feed.entry[i].title.$t.substr(0, showPostTitleAmount);
    }

    /*** LET'S GET THE POST COMMENT COUNT ***/
    if (json.feed.entry[i].thr$total)
    {
      postComment= '<a href="'+json.feed.entry[i].link[j].href+'#comment-form">'+json.feed.entry[i].thr$total.$t+'</a>';
    }

    /*** LET'S GET THE POST AUTHOR INFO ***/
    postAuthor= json.feed.entry[i].author[0].name.$t.split(' ');
    postAuthor=postAuthor.slice(0, 1).join(' ');
    postAuthorImage = json.feed.entry[i].author[0].gd$image.src;

    /*** LET'S GET THE POST CONTENT ***/
    postArticle = json.feed.entry[i].content.$t;
    postContent= postArticle.replace(/(<([^>]+)>)/ig,'').substring(0, showPostContentAmount);

    /*** LET'S FORMAT THE POST DATE MONTH ***/
    postDateFormat= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

    /*** LET'S GET THE POST DATE ***/
    postDate= json.feed.entry[i].published.$t.substring(0,10);

    /*** LET'S DO THE POST DATE FORMAT ***/
    Y = postDate.substring(0, 4);
    m = postDate.substring(5, 7);
    D = postDate.substring(8, 10);
    M = postDateFormat[parseInt(m - 1)];

    /*** LET'S GET THE UPDATED POST DATE ***/
    postUpdated= json.feed.entry[i].updated.$t.substring(0, 16);

    /*** LET'S DO THE UPDATED POST DATE FORMAT ***/
    YY = postUpdated.substring(0, 4);
    mm = postUpdated.substring(5, 7);
    DD = postUpdated.substring(8, 10);
    TT = postUpdated.substring(11, 16);
    MM = postDateFormat[parseInt(mm - 1)];

    /*** LET'S GET THE POST IMAGE ***/
    if (json.feed.entry[i].media$thumbnail)
    {
      postImageURL = json.feed.entry[i].media$thumbnail.url;
      postImageURLSize= postImageURL.replace("/s72-c/","/s"+showInPostImageSize+"/");
      postImage= "'" + postImageURLSize.replace("?imgmax=800","") + "'";
    }

    /*** LET'S ADD VARIOUS IMAGE TYPE SUPPORT***/
    else if (json.feed.entry[i].content.$t.match(/src=(.+?[\.jpg|\.gif|\.png]")/) != null)
    {
      postImage =  json.feed.entry[i].content.$t.match(/src=(.+?[\.jpg|\.gif|\.png]")/)[1];
    }

    /*** LET'S OPEN THE POST ARTICLE ***/
    document.write('<li class="json-post-article-'+[i]+'">');

    /*** LET'S SHOW THE POST CATEGORY ***/
    if (showPostCategory == 'true')
    {
      document.write('<div class="json-post-article-tags"><span class="json-post-article-tags-label"></span>'+postLabel+'</div>');
    }

    /*** LET'S SHOW THE POST IMAGE ***/
    if (showPostImage == 'true')
    {
      document.write('<a class="json-post-article-featured-image-link" href="'+postUrl+'"><figure class="json-post-article-featured-image" style="background: url('+postImage+') no-repeat center center / cover !important"></figure></a>');
    }

    /*** LET'S OPEN THE POST INFO BOX ***/
    document.write('<div>');

    /*** LET'S SHOW THE POST TITLE ***/
    if (showPostTitle == 'true')
    {
      document.write('<h4 class="json-post-article-title"><a class="json-post-article-title-link" href="'+postUrl+'">'+postTitle+'</a></h4>');
    }

    /*** LET'S SHOW THE POST AUTHOR ***/
    if (showPostAuthor == 'true')
    {
      document.write('<span class="json-post-article-author-label"></span><figure class="json-post-article-author-avatar" style="background: url(https:'+postAuthorImage+') no-repeat center center /cover !important"></figure><span class="json-post-article-author-avatar">'+postAuthor+'</span>');
    }

    /*** LET'S SHOW THE POST DATE ***/
    if (showPostDate == 'true')
    {
      document.write('<div class="json-post-article-date"><span class="json-post-article-date-label"></span><span class="json-post-article-date-month">'+M+'</span> <span class="json-post-article-date-day">'+D+'</span> <span class="json-post-article-date-year">'+Y+'</span></div>');
    }

    /*** LET'S SHOW THE POST CONTENT ***/
    if (showPostContent == 'true')
    {
      document.write('<p class="json-post-article-content">'+postContent+'...</p>');
    }

    /*** LET'S SHOW THE POST TAGS ***/
    if (showPostTags == 'true')
    {
      document.write('<div class="json-post-article-tags"><span class="json-post-article-tags-label"></span>'+postLabel+'</div>');
    }

    /*** LET'S SHOW THE POST COMMENT COUNT ***/
    if (showPostComments == 'true')
    {
      document.write('<div class="json-post-article-comment-count"><span class="json-post-article-comment-count-label">Comments: </span><span class="json-post-article-comment-count-number">'+postComment+'</span></div>');
    }

    /*** LET'S CLOSE THE POST INFO BOX ***/
    document.write('</div>');

    /*** LET'S CLOSE THE POST ARTICLE ***/
    document.write('</li>');

  }
  
  /*** LET'S SHOW THE POST CATEGORY ARCHIVE LINK ***/
  if (showCategoryArchive == 'true')
  {
    document.write('<a class="json-post-article-category-more" href="'+domainURL+'/search/label/'+showPostLabel+'">More</a>');
  }

  /*** LET'S STOP THE POST LIST ***/
  document.write('</ul>');

}

/*** LET'S SHOW THE POST AS RANDOM OR ELSE AS RECENT ***/
if (showRandomPosts == 'true')
{
  for (var i = 0; i < postCount ; i++)
  {
  GenerateNum();
  document.write('<script src="https://'+domainURL+'/feeds/posts/default/-/'+showPostLabel+'?alt=json-in-script&start-index='+RandomNum+'&max-results=1&callback=jsonposts"></'+'script>')
  }
}
else 
{
  document.write('<script src="https://'+domainURL+'/feeds/posts/default/-/'+showPostLabel+'?alt=json-in-script&callback=jsonposts"></'+'script>');
}
仅供参考:如果showRandomPosts设置为true,则脚本工作正常

函数
RandomNum()
位于该
if
语句的内部,该语句使函数仅在
showdrandomposts=true


但是如果我理解正确的话,你只想在你的变量为真的情况下显示随机帖子。如果您想继续使用
RandomNum()
函数,您可以删除
/***下的
If
语句,让我们生成随机帖子***/

RandomNum只在函数GenerateNum()内起作用。在这个函数之外,并不存在太多无关紧要的代码。确保问题呈现“最小”复制-构建这样的复制通常可以识别问题本身。@charlietfl已经为您提供了答案。范围界定是这里的问题。。。您正在使用一个超出该范围的函数范围的变量…我的意思是说它是否设置为false,而不是true。不管怎样,我理解你为什么说它不起作用。这是有道理的。然而,我不明白如何将帖子随机设置为可选。