我在构建Javascript对象时遇到问题,并使用setTimeout调用该对象中的方法.我尝试了各种变通方法,但总是在循环的第二部分中,范围变为窗口对象而不是我的自定义对象.警告:我是javascript的新手.
我的代码:
$(function() {
slide1 = Object.create(slideItem);
slide1.setDivs($('#SpotSlideArea'));
slide1.loc = 'getSpot';
slide2 = Object.create(slideItem);
slide2.setDivs($('#ProgSlideArea'));
slide2.loc = 'getProg';
slide2.slide = 1;
setTimeout('triggerSlide(slide1)', slide1.wait);
setTimeout('triggerSlide(slide2)', slide2.wait);
});
function triggerSlide(slideObject) {
slideObject.changeSlide(slideObject);
}
var slideItem = {
div1: null,
div2: null,
slide: 0,
wait: 15000,
time: 1500,
loc: null,
changeSlide: function(self) {
this.slide ? curDiv = this.div1:curDiv = this.div2;
$(curDiv).load(location.pathname + "/" + this.loc, this.slideGo(self));
},
setDivs: function(div) {
var subDivs = $(div).children();
this.div1 = subDivs[0];
this.div2 = subDivs[1];
},
slideGo: function(self) …Run Code Online (Sandbox Code Playgroud) 是否有一个简单的解决方案/想法/策略来在WinForms应用程序中创建一个setTimeout等效函数.我主要是一个Web开发人员,但我不确定如何在WinForms应用程序中进行此操作.基本上,我有一个文本框,每次击键后我想运行一个任务来填充一个列表(比如一个自动完成类型的东西),但是如果用户继续输入字符,希望能够取消(例如clearTimeout)...
我唯一的猜测是可能使用BackGroundWorker并使其初始睡眠,当它处于休眠状态时,它可以被取消,如果用户停止输入键并且睡眠期结束,则它会运行任务等
(我不在乎一个例子是C#还是Vb.Net)
随着Windows系统接近49.7天的正常运行时间,内部Windows毫秒计时器接近2 ^ 32.在计算何时触发setInterval或setTimeout事件时,Internet Explorer 8中的错误似乎有算术溢出.例如,如果您在正常运行时间的第49天,并致电
setInterval(func, 86400000); // fire event in 24 hours
Run Code Online (Sandbox Code Playgroud)
将立即调用func,而不是24小时.
如果将足够大的数字传递给setInterval或setTimeout,则可能在25天正常运行时间(2 ^ 31毫秒)后的任何时间发生此错误.(不过,我只在第49天检查过.)
您可以在命令行中输入"net statistics server"来检查正常运行时间.
有解决方法吗?
我刚开始看javascript,所以希望这会很简单.我想制作一张自动播放的图像幻灯片.这很简单,并且有一些教程,但由于某种原因,我无法让它工作.这就是我所拥有的:
var image1 = new Image();
var image2 = new Image();
var image3 = new Image();
image1.src = "images/website6.jpg";
image2.src = "images/website7.jpg";
image3.src = "images/sunset.jpg";
var images = new Array(
"images/website6.jpg",
"images/website7.jpg",
"images/sunset.jpg"
);
setTimeout("delay(images,0)",2000);
function delay(arr,num){
document.slide.src = arr[num % 3];
var number = num + 1;
setTimeout("delay(arr,number)",1000);
}
Run Code Online (Sandbox Code Playgroud)
我想要改变的图像有id滑动.我也有一些证据表明它有效.会发生什么是第一次图像加载.然后加载第二个图像(这意味着原始的setTimeout调用必须正常工作).然后什么都没发生 对我来说这表明这是递归不起作用.
我对其他语言的递归非常熟悉,所以我认为它必须只是一个语法或其他东西,但我似乎无法弄明白.谢谢你的帮助.
看过许多帖子谈论setTimeout和关闭,但我仍然无法传递一个简单的for循环计数器.
for (i = 0; i < 5; i++) {
setTimeout(function () {
console.log(i);
}, Math.floor(Math.random() * 1000));
}
Run Code Online (Sandbox Code Playgroud)
给
5
5
5
5
5
想拥有
0
1
2
3
4
怎么了 ?
请不要火焰,我以为我已经理解了这个setTimeout()故事,但显然没有.
在Google Drive API的快速入门指南中,一旦加载了客户端库,就会调用以下函数:
// Called when the client library is loaded to start the auth flow.
function handleClientLoad() {
window.setTimeout(checkAuth, 1);
}
Run Code Online (Sandbox Code Playgroud)
setTimeout以这样的延迟1呼叫而不是checkAuth立即呼叫的目的是什么?
我正在构建一个firefox扩展,它创建了几个隐藏的浏览器元素.
我想addProgressListener()为我加载的页面处理onLocationChange.但是,我的处理程序并不总是被调用.
更具体地说,这就是我正在做的事情:
src属性onLocationChange浏览器元素的进度监听器loadURIWithFlags()使用所需的网址调用并发布数据我希望每次在4之后调用处理程序,但有时它不会(虽然它似乎卡在同一页面上).
有趣的是,如果我将3和4包裹在内部,setTimeout(..., 5000);它每次都有效.
我也试过改变一些步骤,但它没有任何效果.
更大的图片:当浏览器contentDocument是新加载的页面(重定向后)时,我希望得到可靠的通知.有一个更好的方法吗?
更新:我已经在mozilla的bug跟踪器上打开了一个错误,其中有一个显示此行为的最小xulrunner应用程序,以防任何人想要仔细查看:https://bugzilla.mozilla.org/show_bug.cgi?id = 941414
我在进行以下测试时遇到此错误:
it('should call pauseAnimationInterval if in focus', inject(function(SearchBoxData, intervalManager, $timeout){
SearchBoxData.init_array = [];
SearchBoxData.inFocus = true;
SearchBoxData.init(intervalManager);
console.log(intervalManager.pauseTimeout);
console.log(intervalManager.pauseTimeoutTime);
console.log($timeout);
$timeout.flush(intervalManager.pauseTimeoutTime+1);
expect(rootScope.$broadcast).toHaveBeenCalledWith('onPauseInterval', intervalManager.loopIndex);
}));
Run Code Online (Sandbox Code Playgroud)
它打破$timeout.flush(intervalManager.pauseTimeoutTime+1);
$ timeout方法在intervalManager.pauseAnimationInterval()里面调用SearchBoxData.init(intervalManager):
intervalManager.pauseAnimationInterval = function (){
intervalManager.safeCancel(intervalManager.continueInterval);
intervalManager.safeCancel(intervalManager.initInterval);
intervalManager.initInterval = null;
intervalManager.continueInterval = null;
intervalManager.pauseTimeout = $timeout(function () {
if(intervalManager.inFocus === true){
intervalManager.loopIndex += 1;
if(intervalManager.loopIndex >= intervalManager.maxLoopIndex){
intervalManager.loopIndex = 0;
}
$rootScope.$broadcast("onPauseInterval", intervalManager.loopIndex);
intervalManager.continueAnimationInterval();
}else{
// Important condition: retry after the timeout if no focus
// main reason of …Run Code Online (Sandbox Code Playgroud) 我正在使用NodeJs并需要调用无限功能,但我不知道什么是最佳性能.
递归函数
function test(){
//my code
test();
}
Run Code Online (Sandbox Code Playgroud)
的setInterval
setInterval(function(){
//my code
},60);
Run Code Online (Sandbox Code Playgroud)
的setTimeout
function test(){
//my code
setTimeout(test,60);
}
Run Code Online (Sandbox Code Playgroud)
我想要最好的性能而不会崩溃服务器.我的代码有几个算术运算.
感谢任何优化javascript性能的建议.
在OpenUI5代码库中,我遇到了这个代码段:
// Wait until everything is rendered (parent height!) before reading/updating sizes.
// Use a promise to make sure
// to be executed before timeouts may be executed.
Promise.resolve().then(this._updateTableSizes.bind(this, true));
Run Code Online (Sandbox Code Playgroud)
看起来正在使用本机Promise函数,没有参数传递给它的resolve函数,它接受:
这个承诺要解决的争论.也可以是Promise或者是一个可以解决的问题.
所以,既然看起来承诺会立即解决并调用then回调,也许意图类似于:
var self = this;
setTimeout(function() {
self._updateTableSizes.bind(self, true)
}, 0);
Run Code Online (Sandbox Code Playgroud)
...基本上,释放JavaScript运行时事件循环来完成其他事情(比如渲染)然后回到回调?
我的问题是:
这是一种常见的模式吗?最佳实践?两种方法都有任何优点/缺点吗?
settimeout ×10
javascript ×8
recursion ×2
setinterval ×2
.net ×1
angularjs ×1
closures ×1
es6-promise ×1
firefox ×1
oop ×1
performance ×1
promise ×1
sapui5 ×1
scope ×1
winforms ×1
xul ×1