我正在为个人需求开发类似脚本的控制台.我需要能够暂停一段时间,但是,根据我的研究,node.js无法按要求停止.经过一段时间后很难读取用户的信息......我已经看到了一些代码,但我相信他们必须在其中包含其他代码才能使其工作,例如:
setTimeout(function() {
}, 3000);
Run Code Online (Sandbox Code Playgroud)
但是,在这段代码之后,我需要在一段时间后执行所有内容.
例如,
//start-of-code
console.log('Welcome to My Console,');
some-wait-code-here-for-ten-seconds..........
console.log('Blah blah blah blah extra-blah');
//endcode.
Run Code Online (Sandbox Code Playgroud)
我也看过像
yield sleep(2000);
Run Code Online (Sandbox Code Playgroud)
但node.js不承认这一点.
我怎样才能实现这种延长的暂停?
我有这样的javascript函数:
function myFunction(number) {
var x=number;
...
... more initializations
//here need to wait until flag==true
while(flag==false)
{}
...
... do something
}
Run Code Online (Sandbox Code Playgroud)
问题是javascript被困在了一段时间并且卡住了我的程序.所以我的问题是如何才能在函数中间等待,直到flag为true而没有"busy-wait"?
我正在尝试使用setTimeout编写一个简单的代码,并且setTimeout不会等待它所设想的时间并且代码立即执行.我究竟做错了什么?
setTimeout(testfunction(), 2000);
Run Code Online (Sandbox Code Playgroud) 简单的说...
为什么
setTimeout('playNote('+currentaudio.id+', '+noteTime+')', delay);
Run Code Online (Sandbox Code Playgroud)
完美地工作,在指定的延迟后调用函数,但是
setTimeout(playNote(currentaudio.id,noteTime), delay);
Run Code Online (Sandbox Code Playgroud)
同时调用函数playNote?
(这些setTimeout()s在for循环中)
或者,如果我的解释太难阅读,这两个函数之间有什么区别?
我有一个函数callWithMagic,它将一个回调函数作为参数,并用一个参数调用它.
const callWithMagic = callback => {
const magic = getMagic();
callback(magic);
};
Run Code Online (Sandbox Code Playgroud)
我还有一个函数processMagic,它有两个参数:magic和theAnswer.
const processMagic = (magic, theAnswer) => {
someOtherMagic();
};
Run Code Online (Sandbox Code Playgroud)
我想将函数processMagic作为参数传递给callWithMagic,但我也希望42将第二个参数传递theAnswer给processMagic.我怎样才能做到这一点?
callWithMagic(<what should I put here?>);
Run Code Online (Sandbox Code Playgroud) 两个功能的故事
我有一个函数填充数组到指定的值:
function getNumberArray(maxValue) {
const a = [];
for (let i = 0; i < maxValue; i++) {
a.push(i);
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
和一个类似的生成器函数,而不是产生每个值:
function* getNumberGenerator(maxValue) {
for (let i = 0; i < maxValue; i++) {
yield i;
}
}
Run Code Online (Sandbox Code Playgroud)
测试跑步者
我已经为这两种情况编写了这个测试:
function runTest(testName, numIterations, funcToTest) {
console.log(`Running ${testName}...`);
let dummyCalculation;
const startTime = Date.now();
const initialMemory = process.memoryUsage();
const iterator = funcToTest(numIterations);
for (let val of iterator) {
dummyCalculation = numIterations - val;
}
const finalMemory = …Run Code Online (Sandbox Code Playgroud) 这有什么区别:
function blankWord(){
console.log('blank!');
setTimeout(blankWord, 5000);
}
blankWord();
Run Code Online (Sandbox Code Playgroud)
每隔5秒就调用一次该函数,这个:
function blankWord(t){
console.log('blank!');
setTimeout(blankWord, t);
}
blankWord(5000);
Run Code Online (Sandbox Code Playgroud)
哪个函数反复疯狂地快速调用?
我正在创建一个可以编辑文本的网页,在您停止输入1秒后,它将自动保存您输入的内容.
目前我正在研究$ timeout细节.当我调用update没有参数的方法时,我有它工作,但当我用params调用它时,我得到错误:
Error: fn is not a function $TimeoutProvider/this.$get</timeout/timeoutId<@http://localhost:63342/express_example/bower_components/angular/angular.js:14014 completeOutstandingRequest@http://localhost:63342/express_example/bower_components/angular/angular.js:4300 Browser/self.defer/timeoutId<@http://localhost:63342/express_example/bower_components/angular/angular.js:4601
Run Code Online (Sandbox Code Playgroud)
为什么我在执行此操作时出现此错误:
timeout = $timeout(update(element, content), 1000);
Run Code Online (Sandbox Code Playgroud)
但不是我这样做的时候:
timeout = $timeout(update, 1000);
Run Code Online (Sandbox Code Playgroud)
显然我需要将params传递给update方法,因为我需要知道要更新的内容.
debounceUpdate($(this), data.content);
var debounceUpdate = function(element, content) {
console.log('in debouce');
if (timeout) {
$timeout.cancel(timeout);
}
timeout = $timeout(update(element, content), 1000);
};
// Update an existing section
var update = function(element, content) {
console.log('in update');
console.log('section_id to update is '+element.data('sectionId'));
console.log(content);
}
Run Code Online (Sandbox Code Playgroud) 我有一些JS代码如下;
var x = self.someAJAXResponseJSON; // x has some object value here
setTimeout(function(x){
console.log("setTimeout ... : " + x); // But x is undefined here
}, 1000);
Run Code Online (Sandbox Code Playgroud)
所以我想将"x"传递给setTimeout回调函数.但我在setTimeout中得到"x"未定义.
我究竟做错了什么 ?
更新
使用DOJO JS修复类似问题的任何想法
setTimeout(dojo.hitch(this, function(){
this.executeSomeFunction(x); // what shud be this
console.log("setTimeout ... : " + x); // But x is undefined here
}), 1000);
Run Code Online (Sandbox Code Playgroud) 我在这里遗漏了什么或者在将函数参数传递给setTimeout调用相同函数时Internet Explorer中是否存在问题?
这将在Internet Explorer中永久运行:
function myFunction(myParam, tries){
if (typeof tries == "undefined"){
tries = 0;
}
tries++;
if (tries < 2){
setTimeout(myFunction, 50, myParam, tries);
}
}
myFunction("something");
Run Code Online (Sandbox Code Playgroud)
有办法解决这个问题吗?
javascript ×10
settimeout ×5
parameters ×2
angularjs ×1
callback ×1
ecmascript-6 ×1
function ×1
generator ×1
jquery ×1
node.js ×1
yield ×1