如何将函数转换为setTimeout

时间:2016-07-20 02:17:07

标签: javascript

我现在使用setInterval,但希望使用setTimeout。

$.when(
            data['layout'] = "getMessages",
            data['limit'] = limit,
            fetchMsg(data,'[data-messages]',{"background-color":"#fff"})
        ).then(

            setInterval(function(){ 
                data['lastupdate'] = localStorage.lastupdate;
                data['layout'] = "getNewMessages",
                fetchMsg(data, '[data-body-new]',{"background-color":"#b4eeb4"});

            }, 1000)
        );

//功能

function fetchMsg(info, container, messageStyle){
            $.ajax({

               method: "POST",
               url: window.root+"/index.php",
               "data": info,
               error: function() {
                 alert('error');
               },  
               success: function(result) {
                  ..........
                 }
              });

我在下面试过,但它不起作用:

setTimeout(function(){ 
    data['lastupdate'] = localStorage.lastupdate;
    data['layout'] = "getNewMessages",
    fetchMsg(data, '[data-body-new]',{"background-color":"#b4eeb4"});                   
}, 1000)

// 功能

function fetchMsg(info, container, messageStyle){
                $.ajax({

                   method: "POST",
                   url: window.root+"/index.php",
                   "data": info,
                   error: function() {
                     alert('error');
                   },  
                   success: function(result) {
                      ..........
                     },
                  complete:fetchMsg
                  });

1 个答案:

答案 0 :(得分:0)

我不会为您修复代码,但这是从间隔到setTimeout的基本概念。

首先,让你的'间隔'等于变量。

var init = setInterval(doFunc,1000);

稍后当您想要进行更改时:

clearInterval(init);

然后设置Timeout就行:

setTimeout(doFunc);

基本上就是这样。

相关问题