如何在第一次迭代时设置一个值,然后在Javascript中永不更改它

如何在第一次迭代时设置一个值,然后在Javascript中永不更改它,javascript,Javascript,我正在尝试实现一个研究栏来学习Meteor、JavaScript等。。。问题是,我想转到与搜索匹配的第一篇文章,但说“有X篇文章符合您的条件” 我试过这个: 'click .tfbutton'(event){ var research = document.getElementById('tftextinput').value.toLowerCase(); var nombreValide = 0; var lastArticlePos = 0;

我正在尝试实现一个研究栏来学习Meteor、JavaScript等。。。问题是,我想转到与搜索匹配的第一篇文章,但说“有X篇文章符合您的条件”

我试过这个:

'click .tfbutton'(event){
      var research = document.getElementById('tftextinput').value.toLowerCase();
      var nombreValide = 0;
      var lastArticlePos = 0;

      setTimeout(function(){
        $('.titreArticle').each(function(i, obj) {
          var acutal = obj.textContent.toLowerCase();
            if(acutal.includes(research)){
                nombreValide = nombreValide + 1;
                var position = obj.offsetTop;
                window.scrollTo(0,position);
                document.getElementById('tftextinput').value = "";
            }else{
               document.getElementById('tftextinput').value = "";
              console.log("We can't find an article with your criterias");
            }
        });
        console.log("There is : " + nombreValide + " articles who matchs");
      }, 20)
      },

我想在第一次迭代时设置
lastArticlePos
,然后再也不更改它,但仍然在
nombreValide
中添加1,只需使用
null
初始化它,并仅在
null
时将其设置为其他值:

'click .tfbutton'(event){
      var research = document.getElementById('tftextinput').value.toLowerCase();
      var nombreValide = 0;
      var lastArticlePos = null;

      setTimeout(function(){
        $('.titreArticle').each(function(i, obj) {
          var acutal = obj.textContent.toLowerCase();
            if(acutal.includes(research)){
                if (lastArticlePos === null) lastArticlePos = i;
                nombreValide = nombreValide + 1;
                var position = obj.offsetTop;
                window.scrollTo(0,position);
                document.getElementById('tftextinput').value = "";
            }else{
               document.getElementById('tftextinput').value = "";
              console.log("We can't find an article with your criterias");
            }
        });
        console.log("There is : " + nombreValide + " articles who matchs");
        console.log("Article position is : " + (lastArticlePos === null) ? "<No article found>" : lastArticlePos);
      }, 20)
      },
'click.tfbutton'(事件){
var research=document.getElementById('tft输出').value.toLowerCase();
var nombreValide=0;
var lastArticlePos=null;
setTimeout(函数(){
$('.titreticle')。每个(函数(i,obj){
var acutal=obj.textContent.toLowerCase();
if(实际包括(研究)){
如果(lastArticlePos==null)lastArticlePos=i;
nombreValide=nombreValide+1;
var位置=对象偏移;
滚动到(0,位置);
document.getElementById('tft输出')。value=“”;
}否则{
document.getElementById('tft输出')。value=“”;
log(“我们找不到符合您标准的文章”);
}
});
log(“有:“+nombreValide+”匹配的文章”);
log(“文章位置为:+(lastArticlePos==null)?”“:lastArticlePos);
}, 20)
},

成功了!谢谢你我本应该考虑这个解决方案,但我没有再次谢谢你!