如何杀死另一个功能

时间:2015-03-07 08:54:19

标签: javascript vk

我有一个功能,可以在视频完成后向API请求:

 video.addEventListener('ended', example);
 var example = function () {
   VK.api('video.get', { owner_id: 123 }, function(data) { 
     /**...*/ 
   }
 }

此外,我还有一个重播按钮(显示视频完成时),用户可以点击比响应来自API的更快。现在我需要杀死我的功能。我怎么能这样做?

API链接:https://vk.com/dev/video.get

2 个答案:

答案 0 :(得分:0)

似乎没有任何方法可以从这个api取消全部。
所以你要么分析代码来看看如何处理那个调用(不能为你做,因为我们需要一个auth)。

我想说你最好的办法就是在拨打电话时将布尔标志设置为true,并在要求重播时将其设置为false:

//Our flag
var userCanceledCalled = false;
var example = function () {
 	document.querySelector('span').innerHTML = "new example should be called in few seconds";
    //Set it to fals at call
	userCanceledCalled = false;
	VK.api('video.get', { owner_id: 123 }, function(data) { 
     if(!userCanceledCalled){
       alert(data);
     }
   });
 }
 
 function replay(){
   //Set it to true on user action
	document.querySelector('span').innerHTML = "new example as been canceled";
 	userCanceledCalled = true;
 	}

var b = document.querySelectorAll('button');
b[0].addEventListener('click', example);
b[1].addEventListener('click', replay);


 var VK={api: function(data, useless, fn){setTimeout(function(){fn(data)}, 3000)}};
<button>Call a new Example</button></br>
<button>Replay</button>
<span id="log"></span>

答案 1 :(得分:-1)

有两种方法:

  1. 取消正在进行的请求
  2. 阻止回调函数执行工作
  3. 我不知道怎么做1.但您可以在Vkontakte问题跟踪器上询问https://vk.com/bugs

    至于第二个,您可以检查用户是否在响应回来之前点击了重播按钮并将您的回调变为noop:

    video.addEventListener('ended', function () {
        VK.api('video.get', { owner_id: 123 }, function (data) {
    
            // Let's check if user has pressed the replay button and
            // if yes then we abort the function:
            if (userHasPressedReplay) { return; }
    
            /**...*/ 
        };
    });
    

    这是不理想的,因为请求仍然命中服务器...但它会阻止执行回调函数。

相关问题