使用setTimeout生成TypeError

时间:2018-03-09 09:36:02

标签: javascript ecmascript-6

我想延迟加载使用setTimeout,我声明一个变量并且它是全局的。但是当我在输入标签中输入内容时它会生成一个TypeError;
我不知道为什么。

以下代码:

var searchInvoiceTimer = null;
clearTimeout(searchInvoiceTimer);
searchInvoiceTimer = setTimeout(function(){
    console.log(1)
},1000)

以下错误:

main.js:14 Uncaught TypeError: timeout.close is not a function
at exports.clearTimeout.exports.clearInterval (main.js:14)
at index.js:540
at Array.map (<anonymous>)
at App.handleTaskChange (index.js:526)
at onChange (index.js:991)
at Object.executeOnChange (LinkedValueUtils.js:132)
at ReactDOMComponent._handleChange (ReactDOMInput.js:241)
at Object.ReactErrorUtils.invokeGuardedCallback (ReactErrorUtils.js:69)
at executeDispatch (EventPluginUtils.js:85)
at Object.executeDispatchesInOrder (EventPluginUtils.js:108)

2 个答案:

答案 0 :(得分:1)

在您的代码检查中,您必须已导入clearTimeout

" import { clearInterval } from 'timers'; 

删除它并使用正常的clearTimeout方法。

答案 1 :(得分:0)

这可能是因为你的IDE。检查您的代码中是否有以下导入。

import { clearInterval } from 'timers';

这是因为某些IDE的自动导入功能。 如果您想禁用此功能,只需在IDE设置中修复它即可。

"typescript.autoImportSuggestions.enabled": false

干杯!