首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置窗口大小调整函数的执行延迟

设置窗口大小调整函数的执行延迟
EN

Stack Overflow用户
提问于 2016-07-22 17:20:39
回答 3查看 412关注 0票数 1

所以我有这个代码来使我的nicEdit响应,基本上是让它在调整窗口大小时重新加载,这样它就可以采用新的尺寸:

代码语言:javascript
运行
复制
$(function () {
    editor = new nicEditor({iconsPath : 'nicedit/nicEditorIcons.gif', fullPanel : true}).panelInstance('myTextarea');
})

$(window).resize(function() {
    editor.removeInstance('myTextarea'); 
    editor = new nicEditor({iconsPath : 'nicedit/nicEditorIcons.gif', fullPanel : true}).panelInstance('myTextarea');
});

现在我想将执行延迟设置为0.5秒或1秒,这样我的浏览器在调整大小时就不会延迟。它的延迟是因为myTextarea实例会被快速移除和重新加载,因为每个像素都会动态调整大小。

我在代码的不同部分尝试了各种形式的setTimeout(check, 1000);,但不知道如何正确使用。

我应该如何去实现这个目标呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-22 17:23:46

您可以使用setTimeout & clearTimeout在每次窗口开始调整大小时重置超时。在窗口停止调整大小后,将调用windowResized()中的代码500ms。

代码语言:javascript
运行
复制
var windowResizeTimeout = null;

$(window).resize(function() {
            // Clears the timeout if it exists
            if (windowResizeTimeout)   clearTimeout(windowResizeTimeout);
            // Sets a new timeout
            windowResizeTimeout = setTimeout(windowResized, 500);
});

function windowResized() {
          // window resized..
          editor.removeInstance('myTextarea'); 
          editor = new nicEditor({iconsPath : 'nicedit/nicEditorIcons.gif', fullPanel : true}).panelInstance('myTextarea');
};

阅读有关clearTimeout() here的更多信息。

票数 2
EN

Stack Overflow用户

发布于 2016-07-22 17:29:10

使用debounce method或前面提到的回答超时。

去抖动方法:

代码语言:javascript
运行
复制
/**
* Execute a function given a delay time
* 
* @param {type} func
* @param {type} wait
* @param {type} immediate
* @returns {Function}
*/
var debounce = function (func, wait, immediate) {
     var timeout;
     return function() {
         var context = this, args = arguments;
         var later = function() {
                 timeout = null;
                 if (!immediate) func.apply(context, args);
         };
         var callNow = immediate && !timeout;
         clearTimeout(timeout);
         timeout = setTimeout(later, wait);
         if (callNow) func.apply(context, args);
     };
};

然后像这样使用它:

代码语言:javascript
运行
复制
$(window).resize(debounce(function(){
    // the following function will be executed every half second
     editor.removeInstance('myTextarea'); 
     editor = new nicEditor({iconsPath : 'nicedit/nicEditorIcons.gif', fullPanel : true}).panelInstance('myTextarea');
},500)); // Milliseconds in which the task should be executed (500 = half second)
票数 0
EN

Stack Overflow用户

发布于 2016-07-22 17:25:09

我不知道你试过什么,但像这样试一下

代码语言:javascript
运行
复制
$(window).resize(function() {

    setTimeout(function() {

        editor.removeInstance('myTextarea');
        editor = new nicEditor({ iconsPath: 'nicedit/nicEditorIcons.gif', fullPanel: true }).panelInstance('myTextarea');

    }, 1000);

});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38522758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档