简体   繁体   English

函数不适用于setTimeout

[英]A Function doesn't work with setTimeout

So the page loads, however a part of the page is fetched from the server, which loads up later, so I set up a setTimeout function, however after doing this the error comes up 因此,页面加载了,但是页面的一部分是从服务器获取的,稍后再加载,因此我设置了setTimeout函数,但是执行此操作后出现错误

Uncaught reference error: check is not defined

I also moved check function outside the setTimeout still no luck 我也将检查功能移到了setTimeout之外仍然没有运气

When this is pasted without the timeout function in the developer console everything, the check function works fine. 如果在开发者控制台的所有内容中都粘贴了没有超时功能的代码,则检查功能可以正常工作。

setTimeout(function() {


    var getclass = document.getElementsByClassName('title');
    var containerlength = getclass.length;

    for (var i = 0; i < containerlength; i++) {
        var container = getclass[i];
        var jobid = getclass[i].children[0].innerHTML.trim().substring(0, 5);
        var fragment = document.createElement('div');
        var chk = "";
        if (localStorage.getItem(jobid) == 1) {
            chk = "checked";
            getclass[i].style.textDecoration = 'line-through';
        }
        fragment.innerHTML = '<input onclick="check(this)" class="jobs" value="' + jobid + '" name="' + jobid + '" type="checkbox"' + chk + '></input>';
        container.appendChild(fragment);

    }

    function check(ele) {
        //     alert(ele.name);
        var name = ele.name;
        var papa = ele.parentNode.parentNode;
        if (localStorage.getItem(name) == 1) {
            localStorage.setItem(name, 0);
            papa.style.textDecoration = 'none';
        } else {
            localStorage.setItem(name, 1);
            papa.style.textDecoration = 'line-through';
        }
    }


}, 5000);

This is probably because you defined check function inside the scope of setTimeout callback. 这可能是因为您在setTimeout回调范围内定义了check函数。 Try to get check function out of the scope, like this: 尝试使检查功能超出范围,如下所示:

function check(ele)
{
    //     alert(ele.name);
    var name = ele.name;
    var papa = ele.parentNode.parentNode;
    if(localStorage.getItem(name) == 1)
    {
        localStorage.setItem(name, 0);
        papa.style.textDecoration = 'none';
    }
    else
    {
        localStorage.setItem(name, 1);
        papa.style.textDecoration = 'line-through';
    }
}


setTimeout(function(){ 

var getclass = document.getElementsByClassName('title');
var containerlength = getclass.length;

for(var i=0; i<containerlength; i++)
{
    var container = getclass[i];
    var jobid = getclass[i].children[0].innerHTML.trim().substring(0, 5);
    var fragment = document.createElement('div');
    var chk = "";
    if(localStorage.getItem(jobid) == 1)
    {
        chk = "checked";
        getclass[i].style.textDecoration = 'line-through';
    }
    fragment.innerHTML = '<input onclick="check(this)" class="jobs" value="' + jobid + '" name="' + jobid + '" type="checkbox"' + chk + '></input>';
    container.appendChild(fragment);

}
}, 5000);

您可以在超时之外编写check函数。由于此函数始终可用,因此可以解决您的问题。

Because you're trying to refer to a global check function, but not the local check . 因为您正在尝试引用全局check功能,而不是本地check

With the current way you build the input element inside the loop you don't have access to the scope where check has been declared. 使用当前的方法在循环内部构建输入元素,您将无法访问已声明check的作用域。

When you've access to the input element reference you can add/set event listeners to it – then the good thing here is to create the input element using document.createElement() , so that you have access to the returned HTML element reference in the same scope where you have access to check . 访问输入元素引用后,可以为其添加/设置事件侦听器-这样做的好处是,使用document.createElement()创建输入元素,以便可以访问中返回的HTML元素引用。您可以check范围相同。

var input = document.createElement('input');
input.addEventListener('click', check.bind(null, input), false);
// This event listener could also be set
// through the 'HTMLElement#onclick' setter

fragment.appendChild(input);

Here's an example of implementation in your code: (I moved check to earlier's scope to free legibility) 这是您代码中的一个实现示例:(我将check移至更早的范围,以实现免费易读性)

var check;

check = function(ele) {
  var name = ele.name;
  var papa = ele.parentNode.parentNode;

  if (localStorage.getItem(name) === 1) {
    localStorage.setItem(name, 0);
    papa.style.textDecoration = 'none';

  } else {
    localStorage.setItem(name, 1);
    papa.style.textDecoration = 'line-through';
  }
}

setTimeout(function() {
  var getclass = document.getElementsByClassName('title');
  var containerlength = getclass.length;

  for (var i = 0; i < containerlength; ++i) {
    var container = getclass[i];

    var jobid = getclass[i]
                  .children[0]
                  .innerHTML
                  .trim()
                  .substring(0, 5);

    var fragment = document.createElement('div');
    var chk = "";

    if (localStorage.getItem(jobid) === 1) {
      chk = "checked";
      getclass[i].style.textDecoration = 'line-through';
    }

    var input = document.createElement('input');

    input.addEventListener('click', check.bind(null, input), false);
    input.class = "jobs";
    input.value =
    input.name = jobid;
    input.type = "checkbox";

    fragment.appendChild(input);
    container.appendChild(fragment);

  }
}, 5000);

Just move the check function outside of the setTimeout and remove the brace on your input. 只需将检查功能移到setTimeout之外,然后删除输入上的括号即可。

Check this plunker 检查这个矮人

https://plnkr.co/edit/sop3R4PqQ13M7DwovdnZ?p=preview https://plnkr.co/edit/sop3R4PqQ13M7DwovdnZ?p=preview

function check(ele) {
        //     alert(ele.name);
        var name = ele.name;
        var papa = ele.parentNode.parentNode;
        if (localStorage.getItem(name) == 1) {
            localStorage.setItem(name, 0);
            papa.style.textDecoration = 'none';
        } else {
            localStorage.setItem(name, 1);
            papa.style.textDecoration = 'line-through';
        }
    }

setTimeout(function() {

    var getclass = document.getElementsByClassName('title');
    var containerlength = getclass.length;

    for (var i = 0; i < containerlength; i++) {
        var container = getclass[i];
        var jobid = getclass[i].children[0].innerHTML.trim().substring(0, 5);
        var fragment = document.createElement('div');
        var chk = "";
        if (localStorage.getItem(jobid) == 1) {
            chk = "checked";
            getclass[i].style.textDecoration = 'line-through';
        }
        fragment.innerHTML = '<input onclick="check(this)" class="jobs" value="' + jobid + '" name="' + jobid + '" type="checkbox"' + chk + '></input>';
    container.appendChild(fragment);

    }
}, 5000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM